0

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);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
CarlosS
  • 161
  • 1
  • 13
  • 3
    This is not c++. This is c++/cli, which is a language targeting .Net. You need to find how to do it using Windows API. – Eugene Nov 27 '16 at 04:54

1 Answers1

4

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks a lot, you´ve oriented me, effectively my application is in a console, so I used setConsoleCtrlHandler to add my HandlerRoutine, wich one works with CTRL_SHUTDOWN_EVENT to detect when Windows is being turned off. – CarlosS Nov 29 '16 at 00:53