I\'m writing a C application which creates a Windows service. I\'d like to check if the service is installed before trying to call the installation function, but I can\'t manage
If you need a yes / no answer whether a service is installed or note, use the following function:
bool IsServiceInstalled(LPWSTR ServiceName)
{
bool serviceInstalled = false;
SC_HANDLE scm_handle = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
if (scm_handle)
{
SC_HANDLE service_handle = OpenService(scm_handle, ServiceName, SERVICE_INTERROGATE);
if (service_handle != NULL)
{
wprintf(L"Service Installed\n");
serviceInstalled = true;
CloseServiceHandle(service_handle);
}
else
{
wprintf(_T("OpenService failed - service not installed\n"));
}
CloseServiceHandle(scm_handle);
}
else
wprintf(_T("OpenService couldn't open - service not installed\n"));
return serviceInstalled;
}