Problem with stopping a service

  • Thread starter Thread starter Tim Gorgs
  • Start date Start date
T

Tim Gorgs

Hi all,

I have some problems with stopping a certain service on my Windows
2000 system. I want to automatically stop the service and then copy a
new version of the service executable over the existing one.

Steps I do to implement this functionality:

Connect to SCM
Connect to service
Send stop request to service via ControlService
Loop QueryServiceStatus until service status changed from
SERVICE_RUNNING to SERVICE_STOPPED
Copy the new service executable over the old one
Restart service

Everything SHOULD be fine as a receive the SERVICE_STOPPED status
after I sent the stop request, but I still can't overwrite the
executable. If I add an additional Sleep() with as less as just a 100
milliseconds, I can overwrite the file. It definately has nothing to
do with filesystem- / user-access rights, the problem just seems to be
that my service reports it's been stopped too quickly.

I've done a lot of research on this topic but unfortunately I couldn't
find anything about it.

Any help is greatly appreciated, thank you!

Tim
 
What you said about the service reporting that it's "stopped too quickly",
sounds about right. When a service is shutting down, it should report
SERVICE_STOPPED, but that doesn't necessarily mean that the last thread of
the process has exited. Once a service reports SERVICE_STOPPED, there's at
least -=some=- code which must be executed (such as CRT cleanup, etc.)
before the process fully exits. Also, I would imagine that the SCM has an
open handle to the process which it uses in a WaitForXXXObjects() call -
this handle takes a minute amount of time to close.

Only after the process has exited, and the last handle to it is closed, can
you replace the executable.

So, in short, the behaviour you're seeing is normal. If the service in
question is one you've written, then you can somewhat minimize the time
between your service reporting SERVICE_STOPPED and the process actually
exiting.
 
Back
Top