Using Windows Services: Program Examples
The following program examples try to demonstrate how to create and control services. The following section lists the program example titles.
Service Program Tasks
Service Configuration Program Tasks
Service Control Program Tasks
Writing a Service Program's main Function Example
The main function of a service program calls the StartServiceCtrlDispatcher() function to connect to the service control manager (SCM) and start the control dispatcher thread. The dispatcher thread loops, waiting for incoming control requests for the services specified in the dispatch table. This thread returns when there is an error or when all of the services in the process have terminated. When all services in the process have terminated, the SCM sends a control request to the dispatcher thread telling it to exit. This thread then returns from the StartServiceCtrlDispatcher() call and the process can terminate. The following global definitions are used in this sample.
#define SVCNAME LSvcName
SERVICE_STATUS gSvcStatus;
SERVICE_STATUS_HANDLE gSvcStatusHandle;
HANDLE ghSvcStopEvent = NULL;
The following code snippet example can be used as the entry point for a service program that supports a single service. If your service program supports multiple services, add the names of the additional services to the dispatch table so they can be monitored by the dispatcher thread. The wmain() function is the entry point. The SvcReportEvent() function writes informational messages and errors to the event log.
#include <windows.h>
#include <stdio.h>
#define SVCNAME LSvcName
SERVICE_STATUS gSvcStatus;
SERVICE_STATUS_HANDLE gSvcStatusHandle;
HANDLE ghSvcStopEvent = NULL;
void WINAPI SvcMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
// Register the handler function for the service
// Code goes here
// These SERVICE_STATUS members remain as set here
// Code goes here
// Report initial status to the SCM
// Code goes here
// Perform service-specific initialization and work
// Code goes here
}
void SvcReportEvent(LPTSTR szFunction)
{
// Code goes here
}
void SvcInstall(void)
{
// Get a handle to the SCM database
// Code goes here
// Create the service
// Code goes here
}
// Purpose: Entry point for the process
// Parameters: None
// Return value: None
int wmain(int argc, WCHAR *argv[])
{
// If command-line parameter is install, install the service.
// Otherwise, the service is probably being started by the SCM.
if( lstrcmpi( argv[1], Linstall) == 0 )
{
wprintf(LInstalling the xyz service....\n);
// Call the service install function
SvcInstall();
}
// TO_DO: Add any additional services for the process to this table.
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain },
{ NULL, NULL }
};
// This call returns when the service has stopped.
// The process should simply terminate when the call returns
if (!StartServiceCtrlDispatcher( DispatchTable ))
{
SvcReportEvent(LStartServiceCtrlDispatcher);
}
return 0;
}
/*
//The following is an example Sample.h as generated by the message compiler
// The following are message definitions.
//
// Values are 32 bit values layed out as follows:
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
//
// where
//
// Sev - is the severity code
//
// 00 - Success
// 01 - Informational
// 10 - Warning
// 11 - Error
//
// C - is the Customer code flag
// R - is a reserved bit
// Facility - is the facility code
// Code - is the facility's status code
//
// Define the facility codes
//
#define FACILITY_SYSTEM 0x0
#define FACILITY_STUBS 0x3
#define FACILITY_RUNTIME 0x2
#define FACILITY_IO_ERROR_CODE 0x4
//
// Define the severity codes
//
#define STATUS_SEVERITY_WARNING 0x2
#define STATUS_SEVERITY_SUCCESS 0x0
#define STATUS_SEVERITY_INFORMATIONAL 0x1
#define STATUS_SEVERITY_ERROR 0x3
//
// MessageId: SVC_ERROR
//
// MessageText:
//
// An error has occurred (%2).
//
//
#define SVC_ERROR ((DWORD)0xC0020001L)
*/