Deleting a Windows service program example using Win32 C code




// For WinXp as a target

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>


int main(void)


SC_HANDLE schSCManager, schService;


// The service name must be a valid name. This fictitious service is available in the running machine...

// It is service name not the displayed name...change accordingly...

LPCTSTR lpszServiceName = L"MyTelnetSrv";

// LPCTSTR lpszServiceName = L"MySampleSrv";


// 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 :o)


if (NULL == schSCManager)

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


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


schService = OpenService(

schSCManager, // SCManager database

lpszServiceName, // name of service

DELETE); // only need DELETE access


if (schService == NULL)

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


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


if (!DeleteService(schService))

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


printf("DeleteService(), %S is deleted.\n", lpszServiceName);


if (CloseServiceHandle(schService) == 0)

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


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

return 0;



Output example:


OpenSCManager() looks OK.

OpenService() looks OK.

DeleteService(), MySampleSrv is deleted.

CloseServiceHandle() looks OK.

Press any key to continue . . .


OpenSCManager() looks OK.

OpenService() looks OK.

DeleteService(), MyTelnetSrv is deleted.

CloseServiceHandle() looks OK.

Press any key to continue . . .


Verify the deleted service. Click Start->Programs->Administrative Tools->Services->Try finding "MyOwnTelnetService" service. It should not be there.


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: Deleting a Windows service C program example

To show: The Windows service programming based on Win32 library



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