Extracting the Windows service information programmatically

 

#include <windows.h>

#include <stdio.h>

 

BOOL GetSampleServiceConfig();

 

int main(void)

{

GetSampleServiceConfig();

return 0;

}

 

BOOL GetSampleServiceConfig()

{

SC_HANDLE schService, schSCManager;

LPQUERY_SERVICE_CONFIG lpqscBuf;

// Service name, service to query

LPCTSTR lpszServiceName = L"NetLogon";

LPSERVICE_DESCRIPTION lpqscBuf2;

DWORD dwBytesNeeded;

BOOL bSuccess=TRUE;

 

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

 

// Open a handle to the service.

schService = OpenService(

schSCManager, // SCManager database

lpszServiceName, // name of service

SERVICE_QUERY_CONFIG);// need QUERY access

 

if (schService == NULL)

{

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

return FALSE;

}

else

printf("OpenService() is OK\n");

 

// Allocate a buffer for the configuration information.

lpqscBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);

if (lpqscBuf == NULL)

{

return FALSE;

}

 

lpqscBuf2 = (LPSERVICE_DESCRIPTION) LocalAlloc(LPTR, 4096);

 

if (lpqscBuf2 == NULL)

{

return FALSE;

}

// Get the Windows service configuration information.

if (! QueryServiceConfig(schService, lpqscBuf, 4096, &dwBytesNeeded))

{

printf("QueryServiceConfig() failed, error: %d", GetLastError());

bSuccess = FALSE;

}

else

printf("QueryServiceConfig() is OK\n");

 

if (!QueryServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, (LPBYTE)lpqscBuf2, 4096, &dwBytesNeeded) )

{

printf("QueryServiceConfig2() failed, error %d", GetLastError());

bSuccess = FALSE;

}

else

printf("QueryServiceConfig2() is OK\n");

 

// Print the configuration information.

printf("\n%S service configuration: \n", lpszServiceName);

printf(" Type: 0x%x\n", lpqscBuf->dwServiceType);

printf(" Start Type: 0x%x\n", lpqscBuf->dwStartType);

printf(" Error Control: 0x%x\n", lpqscBuf->dwErrorControl);

printf(" Binary path: %S\n", lpqscBuf->lpBinaryPathName);

if (lpqscBuf->lpLoadOrderGroup != NULL)

printf(" Load order group: %S\n", lpqscBuf->lpLoadOrderGroup);

if (lpqscBuf->dwTagId != 0)

printf(" Tag ID: %d\n", lpqscBuf->dwTagId);

if (lpqscBuf->lpDependencies != NULL)

printf(" Dependencies: %S\n", lpqscBuf->lpDependencies);

if (lpqscBuf->lpServiceStartName != NULL)

printf(" Start Name: %S\n", lpqscBuf->lpServiceStartName);

if (lpqscBuf2->lpDescription != NULL)

printf(" Description: %S\n", lpqscBuf2->lpDescription);

LocalFree(lpqscBuf);

LocalFree(lpqscBuf2);

return bSuccess;

}

 

 

Output example for Netlogon and SNMP services:

(This program run at the command prompt)

 

F:\vc2005project\cplus\debug>cplus

OpenSCManager(), Open a handle to the SC Manager database looks OK.

OpenService() is OK

QueryServiceConfig() is OK

QueryServiceConfig2() is OK

SNMP service configuration:

Type: 0x10

Start Type: 0x2

Error Control: 0x1

Binary path: C:\WINDOWS\System32\snmp.exe

Load order group:

Dependencies: EventLog

Start Name: LocalSystem

Description: Includes agents that monitor the activity in network devices and report to the network console workstation.

 

 

F:\vc2005project\cplus\debug>cplus

OpenSCManager(), Open a handle to the SC Manager database looks OK.

OpenService() is OK

QueryServiceConfig() is OK

QueryServiceConfig2() is OK

NetLogon service configuration:

Type: 0x20

Start Type: 0x3

Error Control: 0x1

Binary path: C:\WINDOWS\system32\lsass.exe

Load order group: RemoteValidation

Dependencies: LanmanWorkstation

Start Name: LocalSystem

Description: Supports pass-through authentication of account logon events for computers in a domain.

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: Getting a Windows service information

To show: How to program the Windows services using C of the Win32 library

 

 

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