Skip to content

Instantly share code, notes, and snippets.

@the-nose-knows
Last active September 28, 2021 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-nose-knows/607dba810fa7fc1db761e4f0ad1fe464 to your computer and use it in GitHub Desktop.
Save the-nose-knows/607dba810fa7fc1db761e4f0ad1fe464 to your computer and use it in GitHub Desktop.
Winapi ServiceRunning Query
static bool service_running( std::wstring token )
{
SC_HANDLE scm = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT);
if (scm == NULL)
return false;
LPCWSTR lpServiceName = (LPCWSTR)token.data();
SC_HANDLE hService = OpenService(scm, lpServiceName, GENERIC_READ);
if (hService == NULL)
{
CloseServiceHandle(scm);
return false;
}
SERVICE_STATUS status;
LPSERVICE_STATUS pstatus = &status;
if (QueryServiceStatus(hService, pstatus) == 0)
{
CloseServiceHandle(hService);
CloseServiceHandle(scm);
return false;
}
CloseServiceHandle(hService);
CloseServiceHandle(scm);
return (status.dwCurrentState == SERVICE_RUNNING) ? (true) : (false);
}
@nazarred
Copy link

This example works for me! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment