Starting the Windows service programmatically using C code from Win32 library

 

// For WinXp as a target

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

 

BOOL StartSampleService(SC_HANDLE);

 

int main(void)

{

SC_HANDLE schSCManager;

// Open a handle to the SC Manager database...

schSCManager = OpenSCManager(

NULL, // local machine

NULL, // SERVICES_ACTIVE_DATABASE database is opened by default

SC_MANAGER_ALL_ACCESS); // full access rights

 

if (NULL == schSCManager)

printf("OpenSCManager() failed, error: %d.\n", GetLastError());

else

printf("OpenSCManager() looks OK.\n");

 

// Call the StartSampleService()...

StartSampleService(schSCManager);

return 0;

}

 

BOOL StartSampleService(SC_HANDLE schSCManager)

{

SC_HANDLE schService;

SERVICE_STATUS ssStatus;

DWORD dwOldCheckPoint;

DWORD dwStartTickCount;

DWORD dwWaitTime;

// You can try to accept the service name through the command line...

// To test this program, make sure the dhcp client is installed and in the stop state...

LPCTSTR lpszServiceName = L"dhcp";

 

schService = OpenService(

schSCManager, // SCM database

lpszServiceName, // service name

SERVICE_ALL_ACCESS);

 

if (schService == NULL)

{

printf("OpenService() failed, error: %d.\n", GetLastError());

return 0;

}

else

printf("OpenService() looks OK.\n");

 

// Proceed to other task...

if (!StartService(

schService, // handle to service

0, // number of arguments

NULL) ) // no arguments

{

printf("StartService() failed, error: %d.\n", GetLastError());

return 0;

}

else

printf("StartService(), service start pending.\n");

 

// Check the status until the service is no longer start pending.

if (!QueryServiceStatus(

schService, // handle to service

&ssStatus)) // address of status information structure

{

printf("StartService(), service still start pending.\n");

return 0;

}

else

printf("StartService(), service no longer start pending.\n");

 

// Save the tick count and initial checkpoint.

dwStartTickCount = GetTickCount();

dwOldCheckPoint = ssStatus.dwCheckPoint;

 

while (ssStatus.dwCurrentState == SERVICE_START_PENDING)

{

// Just some info...

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

 

// Do not wait longer than the wait hint. A good interval is one tenth the wait hint, but no less than 1 second and no more than 10 seconds...

dwWaitTime = ssStatus.dwWaitHint / 10;

 

if (dwWaitTime <1000> 10000)

dwWaitTime = 10000;

Sleep(dwWaitTime);

 

// Check the status again...

if (!QueryServiceStatus(

schService, // handle to service

&ssStatus)) // address of structure

break;

 

if (ssStatus.dwCheckPoint > dwOldCheckPoint)

{

// The service is making progress...

printf("Service starting in progress...\n");

dwStartTickCount = GetTickCount();

dwOldCheckPoint = ssStatus.dwCheckPoint;

}

else

{

if ((GetTickCount()-dwStartTickCount) > ssStatus.dwWaitHint)

{

// No progress made within the wait hint

printf("Well, starting the service looks no progress...\n");

break;

}

}

}

if (CloseServiceHandle(schService) == 0)

printf("CloseServiceHandle() failed, error: %d.\n", GetLastError());

else

printf("CloseServiceHandle() looks OK.\n");

 

if (ssStatus.dwCurrentState == SERVICE_RUNNING)

{

printf("StartService(), %S service successfully started.\n", lpszServiceName);

return 1;

}

else

{

printf("\nService %S not started.\n", lpszServiceName);

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);

return 0;

}

}

 

Output example: (when the dhcp service in stop mode):

 

OpenSCManager() looks OK.

OpenService() looks OK.

StartService(), service start pending.

StartService(), service no longer start pending.

Wait Hint: 2000

Well, starting the service looks no progress...

CloseServiceHandle() looks OK.

StartService(), dhcp service successfully started.

Press any key to continue . . .

 

Output example: (when the dhcp service is already running):

 

OpenSCManager() looks OK.

OpenService() looks OK.

StartService() failed, error: 1056.

Press any key to continue . . .

 

Verify that the dhcp service has been started.

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C. Click Project menu->your_project_name Properties->Configuration Properties->C/C++->Advanced->Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add Advapi32.lib (Advapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'Advapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Starting the Windows service programmatically using C code

To show: The Windows service programming using C code based on the Win32 library

 

 

C and C++ Programming Resources | C & C++ Code Example Index