Skip to content

Latest commit

 

History

History
90 lines (65 loc) · 2.52 KB

deleting-a-service.md

File metadata and controls

90 lines (65 loc) · 2.52 KB
description ms.assetid title ms.topic ms.date
A service configuration program uses the OpenService function to get a handle to an installed service object. The program can then use the service object handle in the DeleteService function to delete the service from the SCM database.
3bfe4d42-a8a0-4613-9b0f-a80eef54b622
Deleting a Service
article
05/31/2018

Deleting a Service

A service configuration program uses the OpenService function to get a handle to an installed service object. The program can then use the service object handle in the DeleteService function to delete the service from the SCM database.

The DoDeleteSvc function in the following example shows how to delete a service from the SCM database. The szSvcName variable is a global variable that contains the name of the service to be deleted. For the complete example that sets this variable, see SvcConfig.cpp.

//
// Purpose: 
//   Deletes a service from the SCM database
//
// Parameters:
//   None
// 
// Return value:
//   None
//
VOID __stdcall DoDeleteSvc()
{
    SC_HANDLE schSCManager;
    SC_HANDLE schService;
    SERVICE_STATUS ssStatus; 

    // Get a handle to the SCM database. 
 
    schSCManager = OpenSCManager( 
        NULL,                    // local computer
        NULL,                    // ServicesActive database 
        SC_MANAGER_ALL_ACCESS);  // full access rights 
 
    if (NULL == schSCManager) 
    {
        printf("OpenSCManager failed (%d)\n", GetLastError());
        return;
    }

    // Get a handle to the service.

    schService = OpenService( 
        schSCManager,       // SCM database 
        szSvcName,          // name of service 
        DELETE);            // need delete access 
 
    if (schService == NULL)
    { 
        printf("OpenService failed (%d)\n", GetLastError()); 
        CloseServiceHandle(schSCManager);
        return;
    }

    // Delete the service.
 
    if (! DeleteService(schService) ) 
    {
        printf("DeleteService failed (%d)\n", GetLastError()); 
    }
    else printf("Service deleted successfully\n"); 
 
    CloseServiceHandle(schService); 
    CloseServiceHandle(schSCManager);
}

Related topics

Service Installation, Removal, and Enumeration

The Complete Service Sample