Click to See Complete Forum and Search --> : Service


joscollin
November 13th, 2003, 12:31 AM
Hi all,

I wrote a service program.
I want to run my application (Host.exe) when I click Start from services.msc and stop when the service stops.

How can I capture start and stop events from services.msc?

vicodin451
November 13th, 2003, 08:20 AM
Have your service start host.exe when it starts, and have your service stop host.exe when it stops. You can use any number of IPC methods to do this.

joscollin
November 13th, 2003, 12:55 PM
Originally posted by vicodin451
Have your service start host.exe when it starts, and have your service stop host.exe when it stops. You can use any number of IPC methods to do this.

you mean use CreateProcess(), CreateProcessAsUser() etc.
which spawns from ServiceMain?

Please reply..

vicodin451
November 13th, 2003, 12:59 PM
Originally posted by joscollin
you mean CreateProcess(), CreateProcessAsUser() etc.?

Sure.
If host.exe is an app with a UI, then the service could send it a WM_QUIT or WM_CLOSE message when the service is shutting down.

vicodin451
November 13th, 2003, 01:01 PM
http://www.codeguru.com/forum/showthread.php?s=&threadid=270496 might give some ideas...

joscollin
November 13th, 2003, 11:33 PM
Originally posted by vicodin451
http://www.codeguru.com/forum/showthread.php?s=&threadid=270496 might give some ideas...


My application (host.exe) is having sevaral threads.
Also it does the desktop screen capture.
It has a Hook dll used to calculate desktop image updation regions.

There is an api ExitProcess() that will detach dll's before ending the process. I think this is same as sending WM_QUIT or WM_CLOSE message to the process and a normal method too.

joscollin
November 14th, 2003, 07:04 AM
Originally posted by joscollin
My application (host.exe) is having sevaral threads.
Also it does the desktop screen capture.
It has a Hook dll used to calculate desktop image updation regions.

There is an api ExitProcess() that will detach dll's before ending the process. I think this is same as sending WM_QUIT or WM_CLOSE message to the process and a normal method too.

But ExitProcess() is not working
I called ExitProcess() in "case SERVICE_CONTROL_STOP:"
in ServiceHandler(). When I put TerminateProcess() it is working.
My application Host.exe is loading a dll. So I must call ExitProcess().

The service program code is attached

vicodin451
November 14th, 2003, 07:20 AM
Originally posted by joscollin
There is an api ExitProcess() that will detach dll's before ending the process. I think this is same as sending WM_QUIT or WM_CLOSE message to the process and a normal method too.

No, it is not the same as sending WM_QUIT or WM_CLOSE. ExitProcess does not return. When your app calls ExitProcess, it's done.

See http://www.codeguru.com/forum/showthread.php?s=&threadid=269750&highlight=ExitProcess for a discussion about / explanation of why ExitProcess is not a good idea.

vicodin451
November 14th, 2003, 07:29 AM
Originally posted by joscollin
But ExitProcess() is not working
I called ExitProcess() in "case SERVICE_CONTROL_STOP:"
in ServiceHandler(). When I put TerminateProcess() it is working.
My application Host.exe is loading a dll. So I must call ExitProcess().

This doesn't make sense.
A doesn't work. B works. I must call A. ?

ExitProcess operates on the calling application (in this case, your service). TerminateProcess takes a handle to an application that is to be terminated.

See http://www.codeguru.com/forum/showthread.php?s=&threadid=270496&highlight=TerminateProcess or the MSDN doc for information on why TerminateProcess is a bad idea.

Get your app (host.exe) to shut down gracefully. Don't use ExitProcess (???) or TerminateProcess. From your service, tell it when to shut down, and let it handle its own cleanup and termination.

vicodin451
November 14th, 2003, 12:02 PM
Interesting.
Correct me if I'm wrong, but the sole (soul?<g>) purpose of the service is to invoke host.exe? So you need a way to end host.exe, when the service stops due to SERVICE_CONTROL_SHUTDOWN or SERVICE_CONTROL_STOP.

One solution is discussed at:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q178893&


Use EnumWindows() to find the handles to your target windows. In your callback function, check to see if the windows' process ID matches the process you want to shut down. You can do this by calling GetWindowThreadProcessId(). Once you have established a match, use PostMessage() or SendMessageTimeout() to post the WM_CLOSE message to the window.

Side thought: what happens when the user closes host.exe?

joscollin
November 15th, 2003, 12:49 AM
Originally posted by vicodin451
Interesting.
Correct me if I'm wrong, but the sole (soul?<g>) purpose of the service is to invoke host.exe? So you need a way to end host.exe, when the service stops due to SERVICE_CONTROL_SHUTDOWN or SERVICE_CONTROL_STOP.

One solution is discussed at:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q178893&



Side thought: what happens when the user closes host.exe?

It's working. EnumWindows().
But I can't understand how this works without a loop.
for Enumerating all the window handles.

I think Windows does all the job
and calls the CALLBACK function EnumWindowProc()

Thanks for all your suggestions.

regards,
collin

joscollin
November 15th, 2003, 01:25 AM
Originally posted by vicodin451
Side thought: what happens when the user closes host.exe?

When the user closed host.exe the service I have to stop the service. I made an interface program(ServiceInstaller.exe) with create, remove, start, stop services. I think this will do the job. I want to call the ServiceInstaller.exe to stop the service when the user closes Host.exe

thanks,

vicodin451
November 15th, 2003, 11:51 AM
Originally posted by joscollin
It's working. EnumWindows().
But I can't understand how this works without a loop.
for Enumerating all the window handles.

I think Windows does all the job
and calls the CALLBACK function EnumWindowProc()


Right. Windows calls the application-defined callback function supplied in the call to EnumWindows, for each top-level window. You just have to somehow determine which window handle belongs to HOST.EXE.

vicodin451
November 15th, 2003, 11:53 AM
Originally posted by joscollin
When the user closed host.exe the service I have to stop the service. I made an interface program(ServiceInstaller.exe) with create, remove, start, stop services. I think this will do the job. I want to call the ServiceInstaller.exe to stop the service when the user closes Host.exe

thanks,


Another thought: What happens when HOST.EXE is closed by the service, because the service is shutting down?

joscollin
November 16th, 2003, 11:04 PM
Originally posted by vicodin451
Right. Windows calls the application-defined callback function supplied in the call to EnumWindows, for each top-level window. You just have to somehow determine which window handle belongs to HOST.EXE.

I'm using pid from CreateProcess's argument to Identify Host.exe

joscollin
November 16th, 2003, 11:10 PM
Originally posted by vicodin451
Another thought: What happens when HOST.EXE is closed by the service, because the service is shutting down?

I made Host.exe close if the service is shutting down.
For this purpose I'm using EnumWindows() in
SERVICE_CONTROL_STOP and SERVICE_CONTROL_SHUTDOWN cases.

joscollin
November 17th, 2003, 06:02 AM
Originally posted by vicodin451
Another thought: What happens when HOST.EXE is closed by the service, because the service is shutting down?

I want to run Host.exe after lock computer and also after user log off.

I tried the service program and run host.exe as a service
to solve the lock and log off problems. But host.exe is not working after user log off.

please reply..

vicodin451
November 17th, 2003, 07:26 AM
Originally posted by joscollin
I made Host.exe close if the service is shutting down.
For this purpose I'm using EnumWindows() in
SERVICE_CONTROL_STOP and SERVICE_CONTROL_SHUTDOWN cases.
The concern being...

A is shutting down, so it tells B to shut down. B is shutting down so it tells A to shut down. A is shutting down so it tells B to shut down. B is shutting down so it tells A to shut down. Ad nauseam...

vicodin451
November 17th, 2003, 07:27 AM
Originally posted by joscollin
I want to run Host.exe after lock computer and also after user log off.
I was under the impression that if the service was running, host.exe had to be running. If host.exe was running, the service had to be running. Is this not the case?


I tried the service program and run host.exe as a service
to solve the lock and log off problems.
Why are you considering these "problems"?


But host.exe is not working after user log off.

What's "not working"?

joscollin
November 17th, 2003, 09:46 AM
Originally posted by vicodin451
I was under the impression that if the service was running, host.exe had to be running. If host.exe was running, the service had to be running. Is this not the case?


Why are you considering these "problems"?


What's "not working"?

I'm doing a desktop sharing project.
There is a host and client module in this project.
Host is receives messages according to the event that client is generating.

On the client side there is only bitmap display.
Everything happens on host side.

Now i'm facing the problem when the client locks the host computer. Host software is running on the Default desktop.
So when client locks the host computer the winlogon desktop displays and the host stop receiving messages from client.

So when client tries to unlock the host computer it fails.
Actually the host doesnot receive the password characters.

For this reason I planned to run host as a service.
And now the service program and host works on default desktop
but not on winlogon desktop.

Is there any way to run host on winlogon desktop too?

vicodin451
November 17th, 2003, 09:48 AM
Perhaps you could check out the source for VNC?
http://www.realvnc.com