views:

112

answers:

4

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?

+1  A: 

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.

Thomas
A: 

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.

shoosh
A: 

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

zildjohn01
A: 

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.

jn_
Is it working external Hard Driver?
Yigang Wu
Starting from Vista, if the Windows explorer is opened on a network folder with executables that have the SWAPRUN:NET flag set, it also loads the complete executable in memory. If the folder contains many executables, just opening the network folder will cause a serious network overhead.This problem has been confirmed to me by Microsoft, but they don't seem willing to solve this problem.
Patrick