Close my program before Windows shutdown

喜欢而已 提交于 2019-12-25 04:12:37

问题


I´m making a program that must save its files without user interaction when the computer is being turned off. I tried, but I cannot understand how to do it, because most of the info I found is for C#. I found the SystemEvents::SessionEnding event using the below code for C++, but I don´t know how to implement this in dev-c++:

public:
    event SessionEndingEventHandler^ SessionEnding {
        static void add(SessionEndingEventHandler^ value);
        static void remove(SessionEndingEventHandler^ value);
    }
}

回答1:


The code you showed is not C++. It is C++/CLI, aka C++ w/ .NET managed extensions. It only works in Visual Studio.

The plain C/C++ way to do what you are looking for is to:

  1. use a window procedure in a GUI project to handle WM_QUERYENDSESSION and WM_ENDSESSION window messages.

  2. use SetConsoleCtrlHandler() in a console project to handle CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT notifications.

  3. use RegisterServiceCtrlHandlerEx() in a service project to handle SERVICE_CONTROL_PRESHUTDOWN and SERVICE_CONTROL_SHUTDOWN notifications.

Refer to MSDN for details:

Logging Off

Shutting Down



来源:https://stackoverflow.com/questions/40825747/close-my-program-before-windows-shutdown

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