stop the service in c++

Ramamoorthy, Srinath (ext) 31 Reputation points
2022-07-26T14:40:14.58+00:00

I have followed the link for my C++ services

https://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus

if I stop the services in service window
then,

it passes the ServiceCtrlHandler successfully

VOID WINAPI ServiceCtrlHandler (DWORD CtrlCode)
{
switch (CtrlCode)
{
case SERVICE_CONTROL_STOP :

    if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)  
       break;  
 
    /*   
     * Perform tasks necessary to stop the service here   
     */  
      
    g_ServiceStatus.dwControlsAccepted = 0;  
    g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;  
    g_ServiceStatus.dwWin32ExitCode = 0;  
    g_ServiceStatus.dwCheckPoint = 4;  
 
    if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)  
    {  
        OutputDebugString(_T(  
          "My Sample Service: ServiceCtrlHandler: SetServiceStatus returned error"));  
    }  
 
    // This will signal the worker thread to start shutting down  
    SetEvent (g_ServiceStopEvent);  
 
    break;  
 
 default:  
     break;  
}  

}

and then
it hangs in stopping

Windows development | Windows API - Win32
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C++
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-07-27T20:11:30.113+00:00

    It is working fine for me. I am using Microsoft Visual Studio Community 2022.

    Did you download and extract the zip file? Did VS perform an upgrade on the project?

    225405-image.png

    2 people found this answer helpful.
    0 comments No comments

  2. Ramamoorthy, Srinath (ext) 31 Reputation points
    2022-07-28T07:38:26.58+00:00

    225603-stopping.png225646-stopped-sample.pngyes you are right it is working when it is single thread but in case of multithreading?

    In my function I am using multithreading

    https://learn.microsoft.com/en-us/answers/questions/944374/hang-in-stopping.html

    I am using vs 2019

    multithreading: is stopping and hang image

    singlethread is working as expected


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.