asked on
int DoQuerySvc()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS_PROCESS ssp;
DWORD dwBytesNeeded;
TCHAR szSvcName[80];
PSERVICE_NOTIFY pNotifyBuffer = NULL;
HANDLE EventHandle;
SERVICE_NOTIFY* ServiceNotify;
StringCchCopy(szSvcName, 80, L"scardsvr");
// 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)
{
return 0;
}
// Get a handle to the service.
schService = OpenService(
schSCManager, // SCM database
szSvcName, // name of service
SERVICE_ALL_ACCESS); // need query config access
if (schService == NULL)
{
CloseServiceHandle(schSCManager);
return 0;
}
if( !QueryServiceStatusEx(
schService,
SC_STATUS_PROCESS_INFO,
(LPBYTE) &ssp,
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded))
{
goto cleanup;
}
if (ssp.dwCurrentState != SERVICE_RUNNING)
{
memset(&ServiceNotify, 0, sizeof(ServiceNotify));
ServiceNotify->dwVersion = SERVICE_NOTIFY_STATUS_CHANGE;
ServiceNotify->pfnNotifyCallback = onServiceChange;
ServiceNotify->pContext = &EventHandle;
NotifyServiceStatusChange(
schService,
SERVICE_NOTIFY_RUNNING,
ServiceNotify);
WaitForSingleObjectEx(EventHandle, INFINITE);
}
return ssp.dwCurrentState;
cleanup:
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return 0;
static VOID CALLBACK onServiceChange(IN PVOID pParameter)
{
SERVICE_NOTIFY* ServiceNotify;
HANDLE EventHandle;
ServiceNotify = (SERVICE_NOTIFY*) pParameter;
EventHandle = *(HANDLE*)ServiceNotify->pContext;
SetEvent(EventHandle);
}