I have a dialog-based MFC application which needs to stop Windows Wifi service in order to run correctly, but I want to enable it again when my application exits.
So I thought I'd put the code that restarts the service in the destructor of the main dialog class.
Now it has come to my attention that others put their code which should be run during program termination into a WM_CLOSE
message handler.
Both ways seem to work, but I would like to know if there are downsides to either way.
For MFC dialog based application you can place finalization code to application class InitInstance
method, immediately after main dialog DoModal
call. For other MFC application types (MDI, SDI) finalization code is usually placed to ExitInstance
method.
The difference between dialog based application and SDI/MDI applications, is that InitInstance
in dialog based applications returns FALSE, and application exits - all action is done in the main dialog DoModal
call.
You can prefer to use ExitInstance
for all application types, it should work as well.
Edit. If you want to make cleanup code inside of the dialog class, WM_DESTROY
(already mentioned by Roger Rowland) is better place than WM_CLOSE
. Sometimes we can handle WM_CLOSE
message and prevent a dialog to be closed, for example, by asking "Exit the program? Yes - No". In the case you want to use some child windows, they exist in WM_CLOSE
and WM_DESTROY
message handlers, and don't exist in a dialog destructor. Also, message queue doesn't exist when main dialog destructor is called, so you should not use Windows messaging in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With