Solved

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

Posted on 2009-07-07
6
858 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!
0
Comment
Question by:PMH4514
6 Comments
 
LVL 5

Expert Comment

by:iUsername
ID: 24796259
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
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 24798516
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
ID: 24803756
>>>>  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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:lhl60
ID: 25095951
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
 
LVL 4

Expert Comment

by:lhl60
ID: 25095980
0
 

Author Comment

by:PMH4514
ID: 25096909
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now