What is the purpose of creating a C++ service and how to use OpenSCManager and CreateService functions when creating a service?

  • Thread starter Thread starter bhavya internship
  • Start date Start date
B

bhavya internship

Guest
Hello All,


In my windows desktop based application we are creating a C++ service. If the service is running then only the VC++ application executable that is consuming the service is working.

This service is showing under services.msc

I wanted to know what is the purpose of creating a C++ service?

I navigate through the code and understood that we are using the service to add some stuff to the registry. In the code we are using OpenSCManager function and it is to open a handle to the service control manager database. If it is valid handle then we are creating the service using CreateService. Is my understanding is correct?

If it is to add some entries to the registry do we really need to create a service?
Below is the service related code snippet.

ErrCode RegisterService (LPCTSTR aServicePath, LPCTSTR aServiceName, LPCTSTR aDisplayName, LPCTSTR aServiceDescription)
{
ErrCode err;

//Just in case the service is installed allready.
UnRegisterService (aServiceName);

//Get a handle to the Service Manager.
SC_HANDLE schSCManager = OpenSCManager(NULL, NULL,SC_MANAGER_ALL_ACCESS);

if (schSCManager)
{
//Install the new service
SC_HANDLE schService = CreateService(schSCManager,
aServiceName,
aDisplayName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,// PCPE-663 | SERVICE_INTERACTIVE_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
aServicePath,
NULL,
NULL,
NULL,
NULL,
NULL);

if (schService)
{
// Give the service a description ...
SERVICE_DESCRIPTION desc;
desc.lpDescription = LPTSTR(aServiceDescription);

ChangeServiceConfig2 (schService, SERVICE_CONFIG_DESCRIPTION, &desc);

if (StartService(schService, 0, NULL))
{
err = ERR_SUCCESS;
}
CloseServiceHandle(schService);
}

CloseServiceHandle(schSCManager);
}
else
{
assert("RegisterService()" == NULL); //OpenSCManager failed.
}

return err;

}

Regards

Continue reading...
 
Back
Top