Solved

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

Posted on 2009-07-07
6
882 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 49
either24  challenge 19 101
zeroMAx challenge 20 111
Thin secure Windows 10 5 102
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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