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.)
chanmoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
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.
0
chanmoAuthor Commented:
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.
0
chensuCommented:
>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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

chanmoAuthor Commented:
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.
0
chensuCommented:
For NT services, you should call the RegisterServiceCtrlHandler function in your ServiceMain function.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chanmoAuthor Commented:
Thank you very much for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.