Installing a Service
A service configuration program uses the CreateService function to install a service in a SCM database. The following example shows how to install a service. The application-defined schSCManager handle must have SC_MANAGER_CREATE_SERVICE access to the SCManager object. For an example of how to do this, see Opening an SCManager database.
#include <windows.h> #include <stdio.h> BOOL CreateSampleService() { TCHAR szPath[MAX_PATH]; if( !GetModuleFileName( NULL, szPath, MAX_PATH ) ) { printf("GetModuleFileName failed (%d)/n", GetLastError()); return FALSE; } schService = CreateService( schSCManager, // SCManager database TEXT("Sample_Srv"), // name of service lpszDisplayName, // service name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type szPath, // path to service's binary NULL, // no load ordering group NULL, // no tag identifier NULL, // no dependencies NULL, // LocalSystem account NULL); // no password if (schService == NULL) { printf("CreateService failed (%d)/n", GetLastError()); return FALSE; } else { CloseServiceHandle(schService); return TRUE; } }