Querying and getting the Windows service configuration programmatically using C code




// For WinXp as a target

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>


void GetSampleServiceConfig(void);


int main(void)


// Call the function...

printf("In main(), just some test...\n\n");



return 0;



void GetSampleServiceConfig()


// This should be a Subkey name in HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services

// registry key. Here we test querying the winmgmt service...

// Try querying other service...

LPCTSTR lpszServiceName = L"winmgmt";

SC_HANDLE schSCManager, schService;



DWORD dwBytesNeeded;


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


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


// Open a handle to the service.

schService = OpenService(

schSCManager, // SCManager database

lpszServiceName, // name of service



if (schService == NULL)

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


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


// Allocate a buffer for the configuration information.

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


if (lpqscBuf == NULL)

printf("LocalAlloc() for lpqscBuf failed, error: %d.\n", GetLastError());


printf("LocalAlloc() for lpqscBuf looks OK.\n");


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


if (lpqscBuf2 == NULL)

printf("LocalAlloc() for lpqscBuf2 failed, error: %d.\n", GetLastError());


printf("LocalAlloc() for lpqscBuf2 looks OK.\n");


// Get the configuration information.

if (!QueryServiceConfig(






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



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


// May fail and can be scrapped coz can use the following lpqscBuf2->lpDescription...

if (!QueryServiceConfig2(







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



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


// Print the configuration information...

printf("\n%S 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);

if (LocalFree(lpqscBuf) == NULL)

printf("LocalFree() for lpqscBuf is OK.\n");


printf("LocalFree() for lpqscBuf failed.\n");

if (LocalFree(lpqscBuf2) == NULL)

printf("LocalFree() for lpqscBuf2 is OK.\n");


printf("LocalFree() for lpqscBuf2 failed.\n");




Output example:


In main(), just some test...

OpenSCManager() looks OK.

OpenService() looks OK.

LocalAlloc() for lpqscBuf looks OK.

LocalAlloc() for lpqscBuf2 looks OK.

QueryServiceConfig() looks OK.

QueryServiceConfig2() failed, error: 0.

winmgmt configuration:

Type: 0x20

Start Type: 0x2

Error Control: 0x0

Binary path: C

Load order group:

Dependencies: RPCSS

Start Name: LocalSystem

Description: Provides a common interface and object model to access management information about operating system, devices, applications and services. If this service is stopped, most Windows-based software will not function properly. If this service is disabled, any services that explicitly depend on it will fail to start.

LocalFree() for lpqscBuf is OK.

LocalFree() for lpqscBuf2 is OK.

Press any key to continue . . .



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: Querying and getting the Windows service configuration using C code from Win32 library

To show: The  Windows service Win32 functions for querying and getting the Windows service configuration



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