C++ - How to execute a command on application exit?

3.1k views Asked by At

I am very new to C++ and have recently started a project for which I need to access a device, collect certain data and forward it to a datastream on a local network.

While my application does all the things require it lacks on function: When I close the window, in which the application is running it does not stop the hardware-device. The result is, that I have to do a hardware reset every time I am finished with the program. This is not only inconvienient but impossible for the programms intended usage.

I basically just want to set a callback for a function, that is executed, when the program is closed (either by clicking the x, pressing Alt-F4 etc.)

Is this possible? I the possibility to create a handler for such events:

BOOL WINAPI ConsoleHandler(DWORD dwCtrlEvent)
{
    switch (dwCtrlEvent)
    {
    case CTRL_CLOSE_EVENT:
        // something
    case CTRL_SHUTDOWN_EVENT:
        // the same?
    default:
        return FALSE;
    }
}

If this is a correct approach I am wondering how to use this handler? Do I need to create such a handler in my program and the update is constantly?

I am grateful for any help Jonas

4

There are 4 answers

0
Luchian Grigore On BEST ANSWER

Proper RAII usage would help you in this case.

This basically says to wrap resource ownership inside of objects. You can then create an object on program start and clean any resources up on program end:

struct DeviceManager
{
     DeviceManager() { InitDevice(); }
     ~DeviceManager() { DecativateDevice(); }
};

DeviceManager dm;  //namespace scope, single translation unit

dm will be initialized on program start-up, before entry to main() and will be released on program end.

0
Hari Mahadevan On

There's a standard library function atexit, which allows you register a callback to be called when the program exits normally.

To handle abnormal termination, you can employ an exception handler. Simple try{}/catch{} block with the handling code in or after the catch{} should suffice for most simple programs. For advanced setup, refer to structured exception handling here.

0
sfjac On

While you can put in special handlers for various shutdown events, you should consider designing resource control in a more object oriented way, using what is known as RAII (resource acquisition is initialization). This design pattern involves having a class whose creation initializes the underlying device and whose destructor closes and cleans up the underlying device. Thus no matter how the owner of this class is destroyed, the device will be closed.

0
Simon Richter On

I'd make sure that my project is split into the hardware driver, keeping the hardware in a sane state, and the user interface presenting the data to the user.

This way, when the user interface is closed, the driver continues running, cleans up and only then finishes. This also works when the UI is forcibly closed, e.g. at system shutdown, or using the task manager.

You might want to look into the UMDF for more details.