-
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...
-
Re: running a GUI program in the background
so where should i call ShowWindow again in my test program?
and do you mean like this:
Code:
ShowWindow(hWnd, SW_SHOW );
-
Re: running a GUI program in the background
Where you want to.
But you must be sure (you have to test it!) that this second call is really executed.
-
Re: running a GUI program in the background
ok adding the line ShowWindow(hWnd, SW_SHOW ); to the test program causes the window to be displayed when i run createprocess
-
Re: running a GUI program in the background
Good! So now you know a possible reason for the external program to be displayd despite of your startup parameter set to SW_HIDE! :thumb:
-
Re: running a GUI program in the background
ok so how do i solve this problem? what code do i need to hide the window?
-
Re: running a GUI program in the background
Well, the best way would be to change this external program so it will never call ShowWindow with any but SW_SHOWDEFAULT parameter.
It you cannot change this program then you could try with (WH_CBT) HOOK...
-
Re: running a GUI program in the background
i can't change the external program so could you show some code for (WH_CBT) HOOK?
-
Re: running a GUI program in the background
No, I cannot. But you could make a Google search for something like global hook WH_CBT example
-
Re: running a GUI program in the background
ok hook be used in a c++ console application? examples on google only seem to be showing examples in win32
-
Re: running a GUI program in the background
What "c++ console application" are you talking about? What is a problem with a "console application"?
-
Re: running a GUI program in the background
my createprocess program is a console application (the code i put in the first post). can i add hook WH_CBT to this?
-
Re: running a GUI program in the background
No, you need to create a HOOL dll to hook an external applications!
-
Re: running a GUI program in the background
i have never used HOOK before. is there an easier way to hide the window of the external program?
-
Re: running a GUI program in the background
Well, the one and only one way to hide the window is to call ShowWindow with SW_HIDE for this window (or its parent)
So if you know the handle of the main window of this external program then you could try to hide it.
But the question is how often you will have to repeat it! What if this external program tries to maximize or restore its main window every second?
-
Re: running a GUI program in the background
i don't know anything about the code of the external program.. so i guess SW_HIDE isn't a good option then
-
Re: running a GUI program in the background
Okay, it's time to return back to your original design. You say you run the program from your service. Windows Vista/7 service runs in a separate non-interactive session, so any process the service launches remains running in the session, which means no interaction with desktop. Unless you make it run interactive intentionally, which I guess you did not.
What is the OS version you do that run in?
If the OS is XP, do you instruct your service run interactive? If you do, remove the interactive flag.
-
Re: running a GUI program in the background
I wonder if the OP actually has a real service application or is running a pseudo service (like a console application)?
@OP, can you answer if you have created a Windows service application, and if you have are you running it under the System account or under a user account?
Btw, I ask because if you are using a real Windows Service application running under a Service account and it launches a Windows app, you will not see that app run in the logged on user's desktop regardless of whether you have set the app to be shown or not in the CreateProcess call.
-
Re: running a GUI program in the background
Quote:
Originally Posted by
Arjay
Btw, I ask because if you are using a real Windows Service application running under a Service account and it launches a Windows app, you will not see that app run in the logged on user's desktop regardless of whether you have set the app to be shown or not in the CreateProcess call.
Not exactly. I provided sample in the post where service is able to run app in the interactive user session. This is what generally I meant by saying "make it run interactive intentionally."
-
Re: running a GUI program in the background
Quote:
Originally Posted by
Igor Vartanov
Not exactly. I provided sample in
the post where service is able to run app in the interactive user session. This is what generally I meant by saying "make it run interactive intentionally."
I'm aware of running a service interactively. Since that isn't a best practice and only works for the first logged on user, I'm kind of ignoring that special case.
The bottom line is to use the Windows features as intended rather than doing some hack to get things to work (because as we all know, what works as a hack now, may not work in the next release of Windows).
That being said, I don't mean to imply that you are suggesting any sort of hack. :)
-
Re: running a GUI program in the background
There's definitely some misunderstanding there. I told about running interactive process from non-interactive service. And I nevere recommended running services interacting with desktop directly in the same session & window station. I hoped for all the years we're on CG you know me better. :)
-
Re: running a GUI program in the background
Quote:
Originally Posted by
Igor Vartanov
There's definitely some misunderstanding there. I told about running interactive process from non-interactive service. And I nevere recommended running services interacting with desktop directly in the same session & window station. I hoped for all the years we're on CG you know me better. :)
I didn't mean to imply that you had, athough I must admit that my wording wasn't too clear.
-
Re: running a GUI program in the background
i am using window7. i tried setting the service to interact with the desktop but that doesn't work either
-
Re: running a GUI program in the background
Quote:
Originally Posted by
kw1991
i am using window7. i tried setting the service to interact with the desktop but that doesn't work either
What exactly "doesn't work"?
And how did you try to "setting the service to interact with the desktop"?
-
Re: running a GUI program in the background
Quote:
What exactly "doesn't work"?
when i run the service and check the processes that are running (by using task manager) I don't see the GUI program running
Quote:
And how did you try to "setting the service to interact with the desktop"?
In the Service Management Console i changed the log on property to local system account and clicked the interact with desktop box
-
Re: running a GUI program in the background
Quote:
Originally Posted by
kw1991
i am using window7.
If you really want to be helped, you should read and answer all the questions you've been asked in this thread, and be be more verbose in your explanations.
Quote:
Originally Posted by
kw1991
i tried setting the service to interact with the desktop but that doesn't work either
As far as I remember, you complained about program window showing in interactive desktop. So your try made no sense, as what you need is exactly opposite.
-
Re: running a GUI program in the background
ok what questions still need answering? i am using Windows 7, i created a windows service application using c++ visual studio and it uses the local service account
-
1 Attachment(s)
Re: running a GUI program in the background
Then your problem is the program that you launch from your service. And I really doubt you'd be able to play the hook game in this case. :)
See the sample I attach here. Make it, do setup and try test run. The teast.exe process must be present in the list, and its session must be 0 (non-interactive service session).
Then change the service source code to launch you program and see if it starts in session 0. Please remember, we're talking about Windows 7. When you get what you want in Win7, we can start curing your problem in XP.
-
Re: running a GUI program in the background
ok thanks for the code. how do i install the service so it appears in the Service Management Console?
-
Re: running a GUI program in the background
Run setup.bat, as I said above.
-
Re: running a GUI program in the background
ok i tried that and got this message:
[SC] StartService FAILED 2:
The system cannot find the file specified.
-
Re: running a GUI program in the background
Did you build that what you set up? :)
-
Re: running a GUI program in the background
yes it built successfully
-
Re: running a GUI program in the background
the GUI program i am trying to run is a visual basic program
-
Re: running a GUI program in the background
Quote:
Originally Posted by
beginner91
the GUI program i am trying to run is a visual basic program
Well, you already mentioned it... two or three times!
And how could it matter?
Did you test the Igor's sample?
-
Re: running a GUI program in the background
Quote:
Did you test the Igor's sample?
yes and i got this error:
[SC] StartService FAILED 2:
The system cannot find the file specified.