View Full Version : C++: Writing to the log on shutdown
Dunna
07-10-2006, 07:00 PM
Does anyone know of a way for my C++ program to run a specific function when it detects the computer is being shutdown or it is being terminated?
Melon00
07-10-2006, 07:42 PM
You should be able to override the OnClose() function. Though I am speaking from VC++ 6.0 experience. There is also a DestroyWindow() function that is called when the window is destroyed (disappears).
What IDE are you using?
ghell
07-10-2006, 08:53 PM
When the os shuts down it should exit the applications, win32api has window messages (like events) for when the program terminates or window closes etc in the same way that deconstructors are called if a C++ program is closed by pressing the x on a command line (you can call a log write in a destructor and then in your main program just declare the object and do a while(true){} then press the x and it will write the log for example)
I'm not sure if thats what you are looking for but that is what melon was suggesting i think, it involves the program being on when windows is shutting down though and if the program is shut down manually it will still occur.
Dunna
07-11-2006, 07:23 PM
I am using VC++ 6.0. I will try the OnClose() method. I did try the destructor thing, but it writes to the log twice and for some reason fails to work sometimes.
Melon00
07-11-2006, 08:43 PM
Yea, if that doesnt work, I will find another way since you use the same IDE as me. Let me know either way.
ghell
07-11-2006, 08:54 PM
I only mentioned destructors as something else which runs when the application exits as long as it exits gracefully for example being shut down by the os because the os needs to shut down, even in a while(true) loop they will still run if you "x out" the command prompt window.
I have never used MFC i am only familiar with the win32api but there is a windows message WM_CLOSE which i have used like thisLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}(if the message is WM_CLOSE then run DestroyWindow(hwnd) but that could do whatever you want instead)
Melon00
07-12-2006, 04:26 PM
Dunna, ghell's logic will not directly work with VC++. You would have to translate to MFC.
Dunna
01-11-2007, 10:22 PM
Yeah, I realize that but I'm definitely not switching.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.