I have a SW which reside in external HDD, it will run automatically when user plug external HDD, of course, the SW have to close automatically when user unplug the external HDD. I can receive the device lost message, however my SW will not be respond druing releasing all objects, if I call exit(0) directly, the SW will show a crash message. Is there any way to quickly exit SW?
The clean way to exit a Windows application is
PostQuitMessage(0);
This will send WM_QUIT
to the application, causing the message loop to terminate.
The fastest way to terminate a process is to call TerminateProcess()
. This simply stop the process from receiving any further time slices and releases all the resources allocated to it by windows.
This method of ending a process is not recommended though for various reasons. One of the for instance is that if you have an open file which you write to using the standard library, some data may still be buffered and not fully written to the file.
I'll assume you want to exit cleanly. If you don't need to exit cleanly, use PostQuitMessage. If you do, and it's a UI app, send WM_CLOSE to your main window.
You should clarify what objects your software "will not be respond during" when you quit, and why it ceases to respond
I assume you're looking for a way to keep your app from crashing when the user unplugs an external device. You can force the OS to copy your executable from movable media to the swapfile and execute it from there. The corresponding linker switch (for MSVC++) is /SWAPRUN:{NET|CD}.
Set it to CD and the OS will copy the executable to swap when it's executed from the external drive.