Starting a Service

To start a service, the following example opens a handle to an installed database and then specifies the handle in a call to the StartService function. It can be used to start either a service or a driver service, but this example assumes that a service is being started. After starting the service, the program uses the members of the SERVICE_STATUS structure returned by the QueryServiceStatus function to track the progress of the service.

VOID StartSampleService()

{

SERVICE_STATUS ssStatus;

DWORD dwOldCheckPoint;

schService = OpenService(

schSCManager, // SCM database

TEXT("Sample_Srv"), // service name

SERVICE_ALL_ACCESS);

if (schService == NULL)

MyErrorExit("OpenService");

if (!StartService(

schService, // handle to service

0, // number of arguments

NULL) ) // no arguments

{

MyErrorExit("StartService");

}

else

printf("Service start pending\n");

// Check the status until the service is running.

if (!QueryServiceStatus(

schService, // handle to service

&ssStatus) ) // address of status information

MyErrorExit("QueryServiceStatus");

while (ssStatus.dwCurrentState != SERVICE_RUNNING)

{

// Save the current checkpoint.

dwOldCheckPoint = ssStatus.dwCheckPoint;

// Wait for the specified interval.

Sleep(ssStatus.dwWaitHint);

// Check the status again.

if (!QueryServiceStatus(

schService, // handle to service

&ssStatus) ) // address of status information

break;

// Break if the checkpoint has not been incremented.

if (dwOldCheckPoint >= ssStatus.dwCheckPoint)

break;

}

if (ssStatus.dwCurrentState == SERVICE_RUNNING)

printf("StartService SUCCESS\n");

else

{

printf("\nService not started: \n");

printf(" Current State: %d\n",

ssStatus.dwCurrentState);

printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);

printf(" Service Specific Exit Code: %d\n",

ssStatus.dwServiceSpecificExitCode);

printf(" Check Point: %d\n", ssStatus.dwCheckPoint);

printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);

}

CloseServiceHandle(schService);

}

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database