• Welcome to Valhalla Legends Archive.
 

Running C/C++ Applications as a Windows NT/2000/XP/2003 Service

Started by Mephisto, August 06, 2004, 03:15 PM

Previous topic - Next topic

Mephisto

After doing some minor research, I've concluded that you can't just make your C/C++ application run as a service on Windows.  Is this true?  And if so, how do you make your C/C++ do so?  Do you have to make it a specific project (using VS.NET 2003)?  Any links to information sites regarding this, or if someone could explain it to me on this thread would be appreciated...

Adron

Look for service.c and service.h from Microsoft for a good base to build a service from.


Otherwise you can just use the appropriate API calls.

In your service executable, you're supposed to call StartServiceCtrlDispatcher with the information on what services you can run from that executable. In the ServiceMain function for your service, you call RegisterServiceCtrlHandler to register for notifications about stopping, pausing, etc. You also call SetServiceStatus to tell Windows that you're up and running. Or stopping, or failed, or whatever has happened.


To get Windows to load your service, you need to register it, telling it what .exe file to run etc. You use OpenSCManager and CreateService to do that.


This is all well documented in the help files in vs.net.


Adron

Quote from: $t0rm on August 06, 2004, 09:21 PM
srvany will do it!

That works, but if you can change the application to be a service, it will mostly be a much better solution. The stop button on a srvany service (when stopping the service, or when the computer is shutting down) kills the application. Much better if the application can save its things and shut down properly.