chanmo
asked on
A function to execute at program termination?
I'm writing console applications on NT. My program runs in a while loop that it never exits, but I have to do some clean up procedures when the program is killed. I put all these procedures in a clean_up function and use atexit() to achieve this, but when I use Ctrl-C to terminate the program, my clean_up function is not executed. What is the best way to ensure the clean_up function is executed? (The program may be killed in ways other than Ctrl-C also.)
Use the SetConsoleCtrlHandler function. The routine will be called for CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT.
ASKER
Does SetConsoleCtrlHandler() work for NT service program? My program will be turned into NT service eventually. BTW, will this function works with window applications? Thank you.
>Does SetConsoleCtrlHandler() work for NT service program?
Yes, I think so.
>will this function works with window applications?
You mean GUI applications? No, you need to use the WM_KEYDOWN, WM_DESTROY, WM_QUERYENDSESSION and WM_ENDSESSION messages.
Yes, I think so.
>will this function works with window applications?
You mean GUI applications? No, you need to use the WM_KEYDOWN, WM_DESTROY, WM_QUERYENDSESSION and WM_ENDSESSION messages.
ASKER
SetConsoleCtrlHandler() works fine for console program, but not for NT services. The clean up function is not executed when I stopped the NT service in Control Panel.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much for your help!