running a GUI program in the background
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 6 1234 ... LastLast
Results 1 to 15 of 83

Thread: running a GUI program in the background

  1. #1
    Join Date
    Mar 2013
    Posts
    30

    running a GUI program in the background

    i created a windows service that will run another program. but the program i want to run has a gui and i don't want the gui to be visible, i just want the program to run in the background.

    But i have to do it without editing the gui program

    here's my code:
    Code:
            TCHAR* path = L"C:\\Myfile\\test.exe";
    	STARTUPINFO info = {0};
    	PROCESS_INFORMATION processInfo;
    
    	ZeroMemory( &info, sizeof(info) );
    	info.cb = sizeof(info);
    	ZeroMemory( &processInfo, sizeof(processInfo) );
    
    	info.dwFlags = STARTF_USESHOWWINDOW;
    	info.wShowWindow = FALSE; 
    
    	if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
    	{
    		::WaitForSingleObject(processInfo.hProcess, INFINITE);
    		CloseHandle(processInfo.hProcess);
    		CloseHandle(processInfo.hThread);
    	}
        }
    i tested this code with notepad and it runs notepad in the background without displaying the window but when i try run my program it doesn't work. i don't know why its works for one program and not the other..

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    Define "it doesn't work".
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2013
    Posts
    30

    Re: running a GUI program in the background

    it runs my program but it displays the window. i want the window to be hidden

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    Does your program call ShowWindow API? With what argument(s)?
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2013
    Posts
    30

    Re: running a GUI program in the background

    i can't access the code of the other program. i didn't create it so i have no idea about the code
    i think it was made using visual basic

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    Then perform a test: create a very small GUI program (SDI), ensure it calls somewhere the ShowWindow with SW_SHOW or SW_MAXIMIXE or SW_RESTORE parameter, and test your CreateProcess with this program!
    Victor Nijegorodov

  7. #7
    Join Date
    Mar 2013
    Posts
    30

    Re: running a GUI program in the background

    i know the code i posted works because i tested it with notepad and it worked but for some reason it doesn't work with the other program

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    It occusionally "works" with notepad just because notepade does not internally call ShowWindow. (Well, it is IMHO, so, please test if calling ShowWindow(SW_SHOW) would cause the "doesn't work" problem)
    Victor Nijegorodov

  9. #9
    Join Date
    Mar 2013
    Posts
    30

    Re: running a GUI program in the background

    ok i created a win32 project and it has the ShowWindow(hWnd, nCmdShow) function. i ran it through my createprocess code and it works. The windows is hidden and the program is running in the background

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    Could you show the code snippet with ShowWindow?
    Did you test whether this ShowWindow is called?
    Victor Nijegorodov

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    Quote Originally Posted by kw1991 View Post
    i
    Code:
            ...
    
    	if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
    	{
    		::WaitForSingleObject(processInfo.hProcess, INFINITE);
    		CloseHandle(processInfo.hProcess);
    		CloseHandle(processInfo.hThread);
    	}
    I have a question: who and how is considered to close/exit this program? After you have called CloseHandle there is (almost) no more possible to close the program...
    So how long are you going to WaitForSingleObject? Until someone will shut down the PC?
    Victor Nijegorodov

  12. #12
    Join Date
    Mar 2013
    Posts
    30

    Re: running a GUI program in the background

    i just created a blank win32 project in visual studios. i didn't change any code
    so this is the code visual studios generates:
    Code:
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
    
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    Quote Originally Posted by kw1991 View Post
    i just created a blank win32 project in visual studios. i didn't change any code
    so this is the code visual studios generates:
    Well, there is onlz one call of ShowWindow in your example.
    But according to MSDN article STARTUPINFO:
    wShowWindow
    Ignored unless dwFlags specifies STARTF_USESHOWWINDOW. The wShowWindow member can be any of the SW_ constants defined in WINUSER.H. For GUI processes, wShowWindow specifies the default value the first time ShowWindow is called. The nCmdShow parameter of ShowWindow is ignored. In subsequent calls to ShowWindow, the wShowWindow member is used if the nCmdShow parameter of ShowWindow is set to SW_SHOWDEFAULT.
    Victor Nijegorodov

  14. #14
    Join Date
    Mar 2013
    Posts
    30

    Re: running a GUI program in the background

    so i need to change my code that hides the window?
    Code:
    info.dwFlags = STARTF_USESHOWWINDOW;
    info.wShowWindow = FALSE;

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,608

    Re: running a GUI program in the background

    No. Your initial code with CreateProcess is "almost" correct. You should only change it to
    Code:
    info.dwFlags = STARTF_USESHOWWINDOW;
    info.wShowWindow = SW_HIDE;
    You have to change your test program to let it call ShowWindow at least twice with the parameter passed in the second time being NOT SW_SHOWDEFAULT but some of SW_SHOW or SW_MAXIMIXE or SW_RESTORE...
    Victor Nijegorodov

Page 1 of 6 1234 ... LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center