Windows Services Programming 13

 

 

 

 

 

Deleting a Service Example

 

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 code snippet 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.

 

// Purpose: Deletes a service from the SCM database

// Parameters: None

// Return value: None

void __stdcall DoDeleteSvc(void)

{

    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 (schSCManager == NULL)

    {

        wprintf(L"OpenSCManager() failed, error %u\n", GetLastError());

        return;

    }

      else

            wprintf(L"OpenSCManager() is OK lol!\n");

 

    // Get a handle to the service

    schService = OpenService(

        schSCManager,       // SCM database

        szSvcName,          // name of service

        DELETE);            // need delete access

 

    if (schService == NULL)

    {

        wprintf(L"OpenService() failed, error %u\n", GetLastError());

        CloseServiceHandle(schSCManager);

        return;

    }

      else

            wprintf(L"OpenService() is OK!\n");

 

    // Delete the service

    if (!DeleteService(schService))

    {

        wprintf(L"DeleteService() failed, error %u\n", GetLastError());

    }

    else

            wprintf(L"Service was deleted successfully!\n");

 

    CloseServiceHandle(schService);

    CloseServiceHandle(schSCManager);

}

 

Changing a Service's Configuration Example

 

A service configuration program uses the ChangeServiceConfig() and ChangeServiceConfig2() functions to change the configuration parameters of an installed service. The program opens a handle to the service object, modifies its configuration, and then closes the service object handle.

In the following code fragment example, the DoDisableSvc() function uses ChangeServiceConfig() to change the service start type to "Disabled", the DoEnableSvc() function uses ChangeServiceConfig() to change the service start type to "Enabled", and the DoUpdateSvcDesc() function uses ChangeServiceConfig2() to set the service description to "This is a test description". The szSvcName variable is a global variable that contains the name of the service.

 

// Purpose: Disables the service.

// Parameters: None

// Return value: None

void __stdcall DoDisableSvc(void)

{

    SC_HANDLE schSCManager;

    SC_HANDLE schService;

 

    // Get a handle to the SCM database

    schSCManager = OpenSCManager(

        NULL,                    // local computer

        NULL,                    // ServicesActive database

        SC_MANAGER_ALL_ACCESS);  // full access rights

 

    if (schSCManager == NULL)

    {

        wprintf(L"OpenSCManager() failed, error %u\n", GetLastError());

        return;

    }

      else

            wprintf(L"OpenSCManager() is OK!\n");

 

    // Get a handle to the service

    schService = OpenService(

        schSCManager,            // SCM database

        szSvcName,               // name of service

        SERVICE_CHANGE_CONFIG);  // need change config access

 

    if(schService == NULL)

    {

        wprintf(L"OpenService() failed, error %d\n", GetLastError());

        CloseServiceHandle(schSCManager);

        return;

    }

      else

            wprintf(L"OpenService() is fine!\n");

 

    // Change the service start type

    if (!ChangeServiceConfig(

        schService,        // handle of service

        SERVICE_NO_CHANGE, // service type: no change

        SERVICE_DISABLED,  // service start type

        SERVICE_NO_CHANGE, // error control: no change

        NULL,              // binary path: no change

        NULL,              // load order group: no change

        NULL,              // tag ID: no change

        NULL,              // dependencies: no change

        NULL,              // account name: no change

        NULL,              // password: no change

        NULL))            // display name: no change

    {

        wprintf(L"ChangeServiceConfig() failed, error %u\n", GetLastError());

    }

    else

            wprintf(L"Service was disabled successfully.\n");

 

    CloseServiceHandle(schService);

    CloseServiceHandle(schSCManager);

}

 

// Purpose: Enables the service

// Parameters: None

// Return value: None

void __stdcall DoEnableSvc(void)

{

    SC_HANDLE schSCManager;

    SC_HANDLE schService;

 

    // Get a handle to the SCM database

    schSCManager = OpenSCManager(

        NULL,                    // local computer

        NULL,                    // ServicesActive database

        SC_MANAGER_ALL_ACCESS);  // full access rights

 

    if(schSCManager == NULL)

    {

        wprintf(L"OpenSCManager() failed miserably, error %u\n", GetLastError());

        return;

    }

      else

            wprintf(L"OpenSCManager() looks fine!\n");

 

    // Get a handle to the service

    schService = OpenService(

        schSCManager,            // SCM database

        szSvcName,               // name of service

        SERVICE_CHANGE_CONFIG);  // need change config access

 

    if (schService == NULL)

    {

        wprintf(L"OpenService() failed, error %u\n", GetLastError());

        CloseServiceHandle(schSCManager);

        return;

    }   

 

    // Change the service start type

    if (!ChangeServiceConfig(

        schService,            // handle of service

        SERVICE_NO_CHANGE,     // service type: no change

        SERVICE_DEMAND_START,  // service start type

        SERVICE_NO_CHANGE,     // error control: no change

        NULL,                  // binary path: no change

        NULL,                  // load order group: no change

        NULL,                  // tag ID: no change

        NULL,                  // dependencies: no change

        NULL,                  // account name: no change

        NULL,                  // password: no change

        NULL))                // display name: no change

    {

        wprintf(L"ChangeServiceConfig() failed, error %u\n", GetLastError());

    }

    else

            wprintf(L"Service was enabled successfully.\n");

 

    CloseServiceHandle(schService);

    CloseServiceHandle(schSCManager);

}

 

// Purpose: Updates the service description to "This is a test description".

// Parameters: None

// Return value: None

void __stdcall DoUpdateSvcDesc(void)

{

    SC_HANDLE schSCManager;

    SC_HANDLE schService;

    SERVICE_DESCRIPTION sd;

    LPTSTR szDesc = L"This is a test description";

 

    // Get a handle to the SCM database

    schSCManager = OpenSCManager(

        NULL,                    // local computer

        NULL,                    // ServicesActive database

        SC_MANAGER_ALL_ACCESS);  // full access rights

 

    if (schSCManager == NULL)

    {

        wprintf(L"OpenSCManager() failed, error %u\n", GetLastError());

        return;

    }

      else

            wprintf(L"OpenSCManager() looks fine!\n");

 

    // Get a handle to the service

    schService = OpenService(

        schSCManager,            // SCM database

        szSvcName,               // name of service

        SERVICE_CHANGE_CONFIG);  // need change config access

 

    if (schService == NULL)

    {

        wprintf(L"OpenService() failed, error %u\n", GetLastError());

        CloseServiceHandle(schSCManager);

        return;

    }

      else

            wprintf(L"OpenService() is working!\n");

 

    // Change the service description

    sd.lpDescription = szDesc;

 

    if(!ChangeServiceConfig2(

        schService,                 // handle to service

        SERVICE_CONFIG_DESCRIPTION, // change: description

        &sd))                      // new description

    {

        wprintf(L"ChangeServiceConfig2() failed!\n");

    }

    else

            wprintf(L"Service description was updated successfully!\n");

 

    CloseServiceHandle(schService);

    CloseServiceHandle(schSCManager);

}

 

 

 

 

< Windows Services 12 | Win32 Programming | Windows Services Win32 Programming | Windows Services 14 >