HomeProductsCookbookPhotosProgrammingAdminKBBrewLinksSearch
C++ Service AppWizard

Overview

The C++ Service Wizard is a simple App Wizard that emits several classes which form the skeleton of an C++ based NT service. The code for this service is derived from Nigel Thompson's example from the article "Creating a Simple Windows NT Service in C++" in MSDN. For more information on the project's basic architecture, see the aforementioned article at MSDN / Technical Articles / Platform Articles / Networking Articles.

Several additions have been made to allow the service to utilize MFC utility classes such as CString, CSocket, CDatabase, CRecordset, etc… The important thing, here, is that you can't use anything that has to do with windowing (ie: CWnd, CDialog) nor can you do anything with messages, since a service doesn't have a message queue.

Hints

  • Place the unzipped .AWX file in <DevStudioBaseDir>\SharedIDE\Template, then select File / New / Projects / C++ Service Wizard to use it.
  • Place all initialization code in CMyService::OnInit(), and return FALSE if initialization fails. This way the service is prevented from starting, and the user is notified. 
  • The service's main logic belongs in CMyService::Run() inside the while loop. 
  • Service cleanup should be placed at the end of CMyService::Run(), after the while loop. 
  • The debug build runs as a console app rather than a service (handy for debugging purposes). If you want to alter this behavior, change the preprocessor definitions in main().

Step 1 - Project Options

What would you like your derived service class to be called?

The class that inherits from CNTService is where you will do most of your work. This field is where you specify the name of the derived class.

Would you like to use MFC?

This option allows you to specify whether the service will be using any MFC utility classes

Will the service use Winsock?

Click 'Yes, it will' to enable the use of Winsock within the service. If you're using MFC as well, you can't use CAsyncSocket because it requires a message-based callback mechanism, which a service doesn't have. You can, however, use CSocket.

Will the service use MFC database classes?

Click 'Yes, it will' to enable the use of MFC's ODBC classes. In addition, this option places code in your derived class' OnInit member function that opens a connection to the database. You need to edit the login information to match your particular setup. Also, you'll have to add the CRecordset objects yourself.

Step 2 - Service Options

What is the name of the service?

This field is where you specify the actual service name. This is the name that appears in the Control Panel / Services applet. This also determines the location of the service's registry entries.

Should the service be configured for automatic startup?

This option determines how the service's startup option is set at install time. 

Should the service be dependent on another service for startup?

If you need your service to start after another service has already started (ie: MSSQLServer for SQL Server connections), then you should specify that service name here. NOTE: this only applies to services on the local machine.

Download C++ Service AppWizard now


 

1