Cannot stop Windows Service (built in C++ using MSDN sample)

Vadymyr 1 Reputation point
2020-11-23T21:43:01.607+00:00

I used https://learn.microsoft.com/en-us/windows/win32/services/the-complete-service-sample to create a simple service using C++ and Visual Studio 2017.
I can install, start, change the description. But I cannot stop it. I tried one of the solutions found on the internet:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
change DWORD ServicesPipeTimeout to 180000
but that doesn't help.
When I try to stop it using Control Panel\Services, I get:
"Windows could not stop the <servicename> service on Local Computer.
Error 1053: The service did not respond to the start or control request in a timely fashion."
I can provide the full solution, it's x86.
I tried 2 different virtual machines (Windows 7 32-bit Ultimate and 32-bit Starter) and got the same result.
Please help to solve this issue.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,099 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,523 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,636 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RLWA32 43,221 Reputation points
    2020-11-24T03:53:02.207+00:00

    Using HKEY_CURRENT_USER in a service is a mistake. Refer to predefined-keys.

    If I replace the call to ReadStringFromRegistry with a call to OutputDebugString then the service will stop properly. So it seems to me that ReadStringFromRegistry with HKEY_CURRENT_USER is probably the cause of the problem.

    1 person found this answer helpful.