问题
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:
use a window procedure in a GUI project to handle WM_QUERYENDSESSION and WM_ENDSESSION window messages.
use SetConsoleCtrlHandler() in a console project to handle
CTRL_LOGOFF_EVENT
andCTRL_SHUTDOWN_EVENT
notifications.use RegisterServiceCtrlHandlerEx() in a service project to handle
SERVICE_CONTROL_PRESHUTDOWN
andSERVICE_CONTROL_SHUTDOWN
notifications.
Refer to MSDN for details:
Logging Off
Shutting Down
来源:https://stackoverflow.com/questions/40825747/close-my-program-before-windows-shutdown