Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
@deltamish

This comment has been minimized.

Copy link

deltamish commented Oct 15, 2018

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 "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.