Launch function on exit?

Hello.

I've got a program that runs in a command prompt that has some loops and things going on in it. If the program is not closed properly, some things won't stop correctly and the person would have to restart the computer to stop them. So I was wondering if there was a way to have a function launched when the person tries to close the command prompt, as I could have that function stop the other actions before the program closes. Is there such a thing?
That looks promising, but I don't know how to use it, and there's no examples on that page.

I'll better explain what's going on. In my program, the program uses keybd_event() quite a bit. I've got the event for both the downs and the ups in the code, but if someone happens to close the program before the keyup is launched, they'll have to restart the computer so the "button will be release." I have a function that has all the keyup functions in it, I just need it to be called when the program is closed, much like the JavaScript onunload function (http://www.w3schools.com/jsref/jsref_onunload.asp).

After reading through the function you gave me, it looks like it could work, if I knew how to use it. Is there any examples that will show me how to accomplish what I want with that function?

Edit: I figured out how to use the functions. Here's what I have:

#include <iostream>
#include <windows.h>
using namespace std;

BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
if(CEvent == CTRL_CLOSE_EVENT)
{
cout << "You are closing my program.";
}
return TRUE;
}

int main()
{
if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
{
cout << "The handler is not going to work.";
}

int i;

while(1)
{
i = 1;
}
return 0;
}


It almost works, when it gets closed, it displays a message, but then the program crashes instead of just closing afterward, is there another command I need to use to have it close?
Last edited on
did u also read the page when clicking onthe "handler routine" link?;)...

no pain, no gain
1: http://msdn.microsoft.com/en-us/library/ms683242(VS.85).aspx
2: http://msdn.microsoft.com/en-us/library/ms685049(VS.85).aspx

[EDIT] :P...
Last edited on
Read my edit ;)
Oooo.

I didn't see that xD, and I was even googling to see if that was a good method or not xD. Thanks for the support =D.
The console control handler is not a good choice IMHO.
- It has a recognized bug on Win 9x where it is not called when the user Ctrl-click's the X button
- It doesn't catch attempts to simply terminate the program with interrupts like Ctrl-C or from the Task Manager

It is only designed to trap attempts to close the console window -- a thing distinct from terminating an application.

For a simple console application, I recommend using atexit() -- it will catch everything except a forced program termination (from the Task Manager).

The only other choice is to write a windows application...

Hope this helps.
I agree. atexit() is better if you are detecting program exit (as opposed specifically to console closing), which is what you want in your case.
right...

but as far as i see you CAN react on an ctrl+c... and on an "End Task" in the task manager etc...
but ok... if u shoot the process (structure) you can not react ^^...

http://msdn.microsoft.com/en-us/library/ms683242(VS.85).aspx
closed account (z05DSL3A)
Similar (past) discussion here:
http://www.cplusplus.com/forum/general/8112/
The console control handler doesn't prevent the user from closing the console. All it does is give you the option to have Windows present the user with the "End Program" -- "End Now" or "Cancel" dialog.

This is in-line with the way windows applications are supposed to work: when the user tries to close a program, he expects it to close and disappear. GUI programs have the nice feature that you can hook into WM_CLOSE events and ask the user if he really wants to do that. Console applications, however, aren't supposed to know any of this fancy [X] button stuff or how to make dialog windows, etc -- so Windows implements a 'do ya really wanna' dialog for you. You can hook it to moderate its behavior somewhat, but that's all.

The "End Task" button first sends a 'please close' event to the application. If that fails it uses the stuff found with then "End Process" button -- Windows simply terminates the application without further notice. The only reason people will tend to end programs using the task manager is because they can't get windows to do it any other way -- hence, such a shutdown will likely be forcibly... not how your program likes it.

The use of the handler function is more to control your user's "user experience" than the application's shutdown process -- though it is correct that it can be used for that purpose...

Hope this helps.
*confused face*

So... uh... what? xD

Should I just take out my handler and use atexit() instead?

From what I'm reading that wouldn't catch force quits as well. Should I do both? Ahhhh...

I just want to make sure the keys are cancelled when the program ends, whether it's closed, crashes, terminated by task manager, whatever the case. Otherwise the keys are stuck and the user has to restart before they are released.

I don't supposed there's a one does all solution, eh? Or a combination I can use to catch everything. I can learn whatever needs learned (if I pay attention that is), I just need pointed in the right direction.
There's no reason you can't use both. I've done that before.
I'll do that then xD.

Once again thanks for the support.
Topic archived. No new replies allowed.