Bagikan melalui


Menghapus Layanan

Program konfigurasi layanan menggunakan fungsi OpenService untuk mendapatkan handel ke objek layanan yang diinstal. Program kemudian dapat menggunakan handel objek layanan dalam fungsi DeleteService untuk menghapus layanan dari database SCM.

Fungsi DoDeleteSvc dalam contoh berikut menunjukkan cara menghapus layanan dari database SCM. Variabel szSvcName adalah variabel global yang berisi nama layanan yang akan dihapus. Untuk contoh lengkap yang mengatur variabel ini, lihat 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);
}

Penginstalan, Penghapusan, dan Enumerasi Layanan

Sampel Layanan Lengkap