My program need to occupy some resources. I want it to release those resources before exit. It\'s easy to achieve when the excutable exit normally. However, what if the user
It seems impossible to react to the process being killed. The way to do that would be to use signals but as the doc says (here), the SIGTERM signal is not generated under windows NT.
You could try to PInkoke the signal
function and catch SIGINT if your program is a console application and you want to react to ctrl+c.
Like others said, Windows will free up resources for you and the only that is dangerous is that your open files will be left in an unknown state, so it might not be worth the effort.
You cannot handle the scenario if user kill the application forcefully. But you don't have to worry about the resources. Windows will automatically free the resources acquired by your process when it is killed.
When the process is killed the rug is yanked out from under you. But the process's resources will be killed along with the process, so you don't need to clean them up. Ensuring e.g. file consistency, however, is a significantly more complicated story (similar to if the plug were yanked out).