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..
Re: running a GUI program in the background
Define "it doesn't work". :cool:
Re: running a GUI program in the background
it runs my program but it displays the window. i want the window to be hidden
Re: running a GUI program in the background
Does your program call ShowWindow API? With what argument(s)?
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
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!
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
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)
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
Re: running a GUI program in the background
Could you show the code snippet with ShowWindow?
Did you test whether this ShowWindow is called?
Re: running a GUI program in the background
Quote:
Originally Posted by
kw1991
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? :confused:
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;
}
Re: running a GUI program in the background
Quote:
Originally Posted by
kw1991
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:
Quote:
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.
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;
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...