How to catch SERVICE_CONTROL_SHUTDOWN code in Windows Service Program

纵然是瞬间 提交于 2021-02-07 20:30:54

问题


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;

            }
        }

回答1:


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.



来源:https://stackoverflow.com/questions/36193213/how-to-catch-service-control-shutdown-code-in-windows-service-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!