Getting the Windows service status C code sample using Win32 library
// For WinXp as a target
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
BOOL ControlTestService(DWORD fdwControl);
// This example try to stop a service by sending a control code. It ONLY for services that don't
// have dependencies...else it will fail. For services that have dependencies, use the previous example...
int main(DWORD argc, LPCTSTR *argv)
{
SC_HANDLE schService, schSCManager;
SERVICE_STATUS ssStatus;
DWORD fdwAccess;
// Service to be stopped. Change the service name accordingly or try modify this program to accept the service name through command line...
LPCTSTR lpszServiceName = L"MyTelnetSrv";
// Try sending stop control code...
// Notifies a service that it should stop...
DWORD fdwControl = SERVICE_CONTROL_STOP;
// 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, not a good choice
if (NULL == schSCManager)
printf("OpenSCManager(), Open a handle to the SC Manager database failed, error: %d.\n", GetLastError());
else
printf("OpenSCManager(), Open a handle to the SC Manager database looks OK.\n");
// The required service object access depends on the control.
switch (fdwControl)
{
case SERVICE_CONTROL_STOP:
fdwAccess = SERVICE_STOP;
break;
case SERVICE_CONTROL_PAUSE:
case SERVICE_CONTROL_CONTINUE:
fdwAccess = SERVICE_PAUSE_CONTINUE;
break;
case SERVICE_CONTROL_INTERROGATE:
fdwAccess = SERVICE_INTERROGATE;
break;
default:
fdwAccess = SERVICE_INTERROGATE;
}
printf("fdwControl value: 0X%.4x, fdwAccess value: 0X%.4X\n", fdwControl, fdwAccess);
// Open a handle to the service.
schService = OpenService(
schSCManager, // SCManager database
lpszServiceName, // name of service, try stopping other services...
fdwAccess); // specify access
if (schService == NULL)
{
printf("OpenService() failed, error: %d\n", GetLastError());
return FALSE;
}
else
printf("OpenService() is OK\n");
// Send a control value to the service.
if (! ControlService(
schService, // handle to service
fdwControl, // control value to send
&ssStatus) ) // address of status info
{
printf("ControlService() failed, error: %d\n", GetLastError());
return FALSE;
}
else
printf("ControlService() is OK\n");
// Print the service status.
printf("\nStatus of %S: \n", lpszServiceName);
printf(" Service Type: 0x%x\n", ssStatus.dwServiceType);
printf(" Current State: 0x%x\n", ssStatus.dwCurrentState);
printf(" Controls Accepted: 0x%x\n", ssStatus.dwControlsAccepted);
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 examples:
(This program run at the command prompt)
F:\vc2005project\cplus\debug>cplus
OpenSCManager(), Open a handle to the SC Manager database looks OK.
fdwControl value: 0X0001, fdwAccess value: 0X0020
OpenService() is OK
ControlService() is OK
Status of Browser:
Service Type: 0x20
Current State: 0x3
Controls Accepted: 0x1
Exit Code: 0
Service Specific Exit Code: 0
Check Point: 1
Wait Hint: 45000
F:\vc2005project\cplus\debug>cplus
OpenSCManager(), Open a handle to the SC Manager database looks OK.
fdwControl value: 0X0001, fdwAccess value: 0X0020
OpenService() is OK
ControlService() failed, error: 1062
F:\vc2005project\cplus\debug>cplus
OpenSCManager(), Open a handle to the SC Manager database looks OK.
fdwControl value: 0X0001, fdwAccess value: 0X0020
OpenService() is OK
ControlService() is OK
Status of MyTelnetSrv:
Service Type: 0x10
Current State: 0x3
Controls Accepted: 0x7
Exit Code: 0
Service Specific Exit Code: 0
Check Point: 0
Wait Hint: 0
F:\vc2005project\cplus\debug>
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: Extracting the Windows service status programmatically using C language
To show: The Windows service functions available in Win32 library for Windows system programming