简体   繁体   中英

How to catch SERVICE_CONTROL_SHUTDOWN code in Windows Service Program

I'm working on programming a Windows service program recently.

The problem I faced with while programming is that Windows OS doesn't turn off normally when I install my program on it. It takes too long time to turn off the system. It seems my program is the cause for the problem.

I tried to find a solution on the internet, and I found adding 'SERVICE_CONTROL_SHUTDOWN' to my code can be the solution. I did so, but my program doesn't catch 'SERVICE_CONTROL_SHUTDOWN' and I don't know why…

Does anyone know why it is so… and can anyone tell me how to fix it? Thanks.

I put a snippet from my code below.

// method that sets service status. 
void SvcSetStatus(DWORD dwState, DWORD dwAccept = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN )
        {
            SERVICE_STATUS ss;
            ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
            ss.dwCurrentState = dwState;
            ss.dwControlsAccepted = dwAccept;
            ss.dwWin32ExitCode = 0;
            ss.dwServiceSpecificExitCode = 0;
            ss.dwCheckPoint = 0;
            ss.dwWaitHint = 0;


            g_NowState = dwState; 
            SetServiceStatus(g_hSrv, &ss);

        }

// method of service handler 
void SvcHandler(DWORD fdwControl)
        {

            if (fdwControl == g_NowState)
            {
                return;

            }

            switch (fdwControl)
            {
                case SERVICE_CONTROL_PAUSE:

                    break;
                case SERVICE_CONTROL_CONTINUE:

                    break;
                case SERVICE_CONTROL_STOP:
                    ShutdownService(FALSE);
                    break;
                case SERVICE_CONTROL_INTERROGATE:
                    break;
                case SERVICE_CONTROL_PRESHUTDOWN:

                    ShutdownService(TRUE); //???
                    break;
                case SERVICE_CONTROL_SHUTDOWN:

                    ShutdownService(TRUE); //service finalize function (parameter is for shutdown or not)

                    break;
                default:
                    SvcSetStatus(g_NowState);
                    break;

            }
        }

The problem I faced with while programming is that Windows OS doesn't turn off normally when I install my program on it. It takes too long time to turn off the system. It seems my program is the cause for the problem.

This usually means that you are not reporting status correctly during SCM stop/shutdown requests. You did not show your code for ShutdownService() , but given the way you have coded SvcHandler() , make sure that ShutdownService() calls SvcSetStatus(SERVICE_STOP_PENDING) periodically while the service is in the process of stopping, and calls SvcSetStatus(SERVICE_STOPPED) once the service has fully stopped.

Also, another possible cause of the hang could be if your service has created a top-level HWND for itself and its window procedure is not responding to unhandled messages correctly, such as by calling DefWindowProc() . During shutdown, certain messages get broadcasted to top-level windows, even in service processes, and they need to be responded to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM