Usuwanie usługi
Program konfiguracji usługi używa funkcji OpenService w celu uzyskania dojścia do zainstalowanego obiektu usługi. Program może następnie użyć dojścia obiektu usługi w funkcji DeleteService, aby usunąć usługę z bazy danych SCM.
Funkcja DoDeleteSvc w poniższym przykładzie pokazuje, jak usunąć usługę z bazy danych SCM. Zmienna szSvcName to zmienna globalna zawierająca nazwę usługi do usunięcia. Pełny przykład ustawiający tę zmienną można znaleźć w 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);
}
Tematy pokrewne