Set Windows Service Description in C++

2020-02-28 00:09发布

问题:

I am using CreateService to installs a Windows Service executable however I can't seem to find out how to set the description for the service.

Does anyone know how to do this?

Thanks.

回答1:

Call ChangeServiceConfig2 passing SERVICE_CONFIG_DESCRIPTION as the dwInfoLevel parameter. You will also need a handle to the service, but CreateService gives you one of those.

SERVICE_DESCRIPTION description = { L"The service description" };
ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &description);


回答2:

Have a look at this MSDN page for an example. You use the ChangeServiceConfig2 method.

SERVICE_DESCRIPTION sd;
SC_HANDLE schService;
SC_HANDLE schSCManager;

// Not shown: Get a handle to the SCM database. 
// Not shown: Get a handle to the service.

sd.lpDescription = TEXT("Description");
ChangeServiceConfig2( schService,                 // handle to service
                      SERVICE_CONFIG_DESCRIPTION, // change: description
                      &sd) )                      // new description


标签: c++ unmanaged