Skip to content

Commit

Permalink
This is a scratch sample of checking if the service is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Oct 29, 2020
1 parent 4daed9d commit dd0b8b2
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/tools/scratch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,86 @@
// Licensed under the MIT license.

#include <windows.h>
#include <stdio.h>

// This wmain exists for help in writing scratch programs while debugging.
int __cdecl wmain(int /*argc*/, WCHAR* /*argv[]*/)
{
printf("Getting manager...\n");
auto hManager = OpenSCManager(nullptr, nullptr, SERVICE_QUERY_CONFIG);
// printf("OpenSCManager returned %d\n", static_cast<unsigned long long>(hManager));
if (hManager == 0)
{
auto gle = GetLastError();
printf("gle: %d", gle);
return gle;
}

printf("Getting service...\n");
auto hService = OpenService(hManager, L"TabletInputService", SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS);
// printf("OpenService returned %d\n", static_cast<unsigned long long>(hService));
if (hService == 0)
{
auto gle = GetLastError();
printf("gle: %d", gle);
return gle;
}

DWORD dwBytesNeeded = 0;
LPQUERY_SERVICE_CONFIG lpsc = nullptr;

printf("Getting config size...\n");
if (!QueryServiceConfig(hService,
NULL,
0,
&dwBytesNeeded))
{
auto gle = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == gle)
{
auto cbBufSize = dwBytesNeeded;
lpsc = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LMEM_FIXED, cbBufSize);
}
else
{
printf("QueryServiceConfig failed (%d)", gle);
return gle;
}
}

printf("Getting config...\n");
if (!QueryServiceConfig(hService,
lpsc,
dwBytesNeeded,
&dwBytesNeeded))
{
auto gle = GetLastError();
printf("QueryServiceConfig failed (%d)", gle);
return gle;
}

printf("Succeeded!\n");

printf("Start Type: 0x%x\n", lpsc->dwStartType);

SERVICE_STATUS status{ 0 };
if (!QueryServiceStatus(hService, &status))
{
auto gle = GetLastError();
printf("QueryServiceStatus failed (%d)", gle);
return gle;
}
printf("State: 0x%x\n", status.dwCurrentState);

auto state = status.dwCurrentState;
if (state != SERVICE_RUNNING && state != SERVICE_START_PENDING)
{
printf("The service is stopped\n");
}
else
{
printf("The service is running\n");
}

return 0;
}

0 comments on commit dd0b8b2

Please sign in to comment.