Last active
September 28, 2021 20:13
-
-
Save the-nose-knows/607dba810fa7fc1db761e4f0ad1fe464 to your computer and use it in GitHub Desktop.
Winapi ServiceRunning Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
I tried your code and it seems to throw a
"Lnk 2028 Error :error LNK2028: unresolved token (0A0001BB) "extern "C" int stdcall QueryServiceStatus(struct SC_HANDLE *,struct SERVICE_STATUS *)" (?QueryServiceStatus@@$$J18YGHPAUSC_HANDLE_@@PAU_SERVICE_STATUS@@@z) referenced in function "bool __cdecl `anonymous namespace'::service_running(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >)" (?service_running@?A0xb71d0be1@@$$FYA_NV?$basic_string@_WU?$char_traits@_W@std@@v?$allocator@_W@2@@std@@@z) Test_Service.obj "