• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 953
  • Last Modified:

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

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)

1 Solution
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.
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?
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
>>>>  want to hope that this srvany.exe does not call TerminateProcess.

It does. Here the excerpt from svrany.wri

When the service is stopped, it terminates the application via the WIN32 TerminateProcess() API: this is a drastic way to end an application. For example, it would not allow the application to prompt the user to save changes. Therefore, it is recommended to close the application BEFORE stopping the service.

To come around that, you might write a little launcher app which starts your app using CreateProcess and wait for termination using WaitForSingleObject. Then your app would create a thread in YourApp::InitInstance and let the thread wait for the process handle of the launcher. If the srvany kills the launcher the thread would goon and can make an orderly closing of the app, e. g. by posting a WM_CLOSE messages both to the active dialog and the mainframe window (if any).
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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,


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.

PMH4514Author 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?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now