Thread: make Win32 Console into background app

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Unhappy make Win32 Console into background app

    I've written a program in C in Visual C++ 2008. Basically the program read a txt file (located on the same folder of exe) and transfer the data in it to MySQL server. Right know I've only make it run once,later I intend to put it in a while loop.

    I want to make this program runs in the background.From google I've found for example about using Windows Service,WinMain(),freeconsole(),etc...
    I'm not sure about Windows Service bcos i'm afraid i'll messing around with register and stuff.
    Is there any simpler way?

    I'll use this program on XP or Vista.I hope it doesn't take much memory.My program doesn't require any input from keyboard(user).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Clusty Search ยป run as service
    Some of them might even be free.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The easiest way to do this, is just create a windows program instead of a console program. Then make your main window invisible.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    from my research, i could use Windows Service or CreateProcess function.

    today i spent the whole day at work (literally) trying to find any complete example code in c for both.
    it really amazed me how little examples are available on the net!
    even the CreateProcess function is very,very tricky and difficult to understand,which i assume at first could be easier than making it a service.

    after trying a few CreateProcess examples,none of them compilable..I'll end up with linking error and then when i googled,i received many confusing answers...such as,"change to Win32 API not console,c++,Unicode,add #include <tchar.h>,error LNK2019,WinMain,_main...bla.bla..change this,that!"

    msdn documentation is very poor.

    the only compilable and working example is this from msdn but i simply don't know how to connect/make my console program a process:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>/*i have to add this,otherwise error LNK2019...very weird*/
    
    
    void _tmain( int argc, TCHAR *argv[] )
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
    
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );
    
        if( argc != 2 )
        {
            printf("Usage: %s [cmdline]\n", argv[0]);
            return;
        }
    
        // Start the child process. 
        if( !CreateProcess( NULL,   // No module name (use command line)
            argv[1],        // Command line
            NULL,           // Process handle not inheritable
            NULL,           // Thread handle not inheritable
            FALSE,          // Set handle inheritance to FALSE
            0,              // No creation flags
            NULL,           // Use parent's environment block
            NULL,           // Use parent's starting directory 
            &si,            // Pointer to STARTUPINFO structure
            &pi )           // Pointer to PROCESS_INFORMATION structure
        ) 
        {
            printf( "CreateProcess failed (%d).\n", GetLastError() );
            return;
        }
    
        // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );
    
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    }
    the ouput is:
    Usage: c [cmdline]
    press any key to continue...
    and then what?! i simply angry with this bcos i have the most unproductive day!
    Last edited by gelbegeld; 07-21-2009 at 09:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console App w/ Threads and Events?
    By sean in forum C# Programming
    Replies: 1
    Last Post: 07-02-2004, 12:16 AM
  2. Win32 Console App... multiple console buffers
    By Trauts in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2003, 11:26 AM
  3. Scroll in win32 console mode
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 04-28-2002, 07:02 AM
  4. Displaying BMP in DJGPP (Win32 Console)
    By CBGMan in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2001, 01:59 PM
  5. Turning a Console APP into Windows.
    By Darkflame in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2001, 03:10 AM

Tags for this Thread