I have an Win32 application with no window written in C. My question is: is there any way to handle the termination of my application. Ex. closing it from the task manager or via the console.
相关问题
- Multiple sockets for clients to connect to
- the application was unable to start correctly 0xc0
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Handle button click in another application
You might like to take a look at the
atexit()
function (http://msdn.microsoft.com/en-us/library/tze57ck3%28v=vs.100%29.aspx).Using this function you can install handlers which are called when the program terminates.
It is unclear from the question, but if this is a console mode application then you can call SetConsoleCtrlHandler to install a callback that Windows will call just before it terminates your app. Beware that this callback runs on a separate thread and that you have to complete the callback function quickly.
If it is a native Windows program that just doesn't create a window then you really do need a window to get notifications like this. Which is not a problem, it doesn't have to be visible. Just don't call ShowWindow().
Note that atexit() as mentioned will not work, these are rude aborts you are talking about that don't let the program go through its normal shutdown sequence.