Using the OpenSCManager(), a service control manager function



// For WinXp as a target

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>


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


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

return 0;



Output example:


OpenSCManager() looks 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: Using the OpenSCManager(), a service control manager Win32 function

To show: The Windows process and service Win32 C code sample



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