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)

Thanks!
PMH4514Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iUsernameCommented:
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.
0
pgnatyukCommented:
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
 
0
itsmeandnobodyelseCommented:
>>>>  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).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

lhl60Commented:
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.

0
lhl60Commented:
0
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.