We help IT Professionals succeed at work.

C++ 6.0 MFC app running as service, and ExitInstance

1,017 Views
Last Modified: 2013-11-20
I have a small application built with VC++ 6.0 that historically ran as a standalone executable, a small dialog on the desktop.  When one clicks File->Close or the X in the upper right,  CMyApp::ExitInstance() is called, allowing me to shut everything down in the proper order. This all works just as it should. All computers are running on XP Pro SP3

We are now installing it as a service. We're using 'srvany.exe' to set it up as a service and for the most part this too seems to work just fine.

However, it seems that when the user shuts down the computer (Start Menu->Shut Down), or, simply stops the service in the services dialog, my logging is showing that ExitInstance is not being called.

Is there a hook or something I can add to my MFC project so that ExitInstance is called, and fully executed, when the service is stopped? (which I would assume would cover system shut-down)

Thanks!
Comment
Watch Question

When a service is going down, it is calling the "service_control_handler" with the SERVICE_CONTROL_STOP DWORD flag.

I guess that srvany.exe is killing the the application you gave it when this flag is being called.

So, one thing you can do, is try to see if there is a way using srvany to get that flag and call the function you want.

Another option that MIGHT work (I'm just guessing here), is to call ExitInstance() in the destructor of the dialog.

Commented:
I want to hope that this srvany.exe does not call TerminateProcess. Many people use this app.
If that does happen, you will have memory/GDI/other leaks. So the right (but, probably unacceptable?) way is to write this service yourself. You said it is a small application.
You can to find a way to clean the ExitInstance (move the code from it to somewhere else), if it is possible.
Maybe, this is an option to make a small app for your users that will close the app running like a service (people say that in this case the process will still exist).
This srvany is from Microsoft Resource Kit (a kind of a virus :)): http://support.microsoft.com/kb/137890
Here you can find a recommandation:
For more information on installing and removing a user-defined service, please see the Srvany.wri document provided with the Windows NT Resource Kit utilities (i.e., C:\Reskit\Srvany.wri). This document can also be found on the Windows NT Resource Kit CD in the Common\Config directory.  
Here is another link about the same but with screenshots: http://www.tacktech.com/display.cfm?ttid=197
You made new service, updated the registry, right?
To remove the service: instsrv.exe NEWSERVICE remove - but this is not the case, right?
So, maybe:
TASKKILL /F /IM srvany.exe?
or
net stop <service name>
Here is an alternative app (shareware) with ability to terminate up to 2 processes: http://www.safe-install.com/programs/remote-service-installer.html
ServiceShell: http://wareseeker.com/download/serviceshell-1.3.rar/403483
 
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Jus like itsmeandnobodyelse suggest write a wrapper prgram.

but instead of using svrany,  why not simply write your own REAL installable service,
that simply wraps your program. ( instantiate Cwinapp::YourApplication)
The you will get all the callbacks you need, and can forward them to you app the propper way
It' not difficult to writre a servcie ( basically it just looks like a lot of activations of you program with different event codes (start/stop/paus...) , a good starting point for writing services is,

 http://msdn.microsoft.com/en-us/library/ms686315(VS.85).aspx

I  did that for several programs, I can't find the code right now, but I'll keep looking, and return if I find it.

Author

Commented:
Excuse me folks, I somehow missed this was still an open question! I was able to re-write the small program as a proper service which was technically pgnatyuk's first response, but  everybody provided valuable info for this one.  Mind if I split up points?
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.