I am spawning an application from c++ with ShellExecute, so I have the HINSTANCE of the app.
How 开发者_开发问答can I close it now using that HINSTANCE? And can I use WaitForSingleObject() to wait for the app to finish?
There is no way to gracefully shutdown a process. There is an abrupt an non-graceful way by using TerminateProcess
, but this will prevent the process from running its own cleanup code and may leave stuff 'in-limbo', see Quick overview of how processes exit on Windows XP.
For a long time now the common wisdom about 'terminating a process' was to send a WM_CLOSE to the process topmost unowned window(s), in expectation that the application will respond by quitting gracefully. See How To Terminate an Application "Cleanly" in Win32, and also this mandatory reading on the related subject of finding the right window: There can be more than one (or zero): Converting a process to a window
Ans you should also read What can I do with the HINSTANCE returned by the ShellExecute function? to understand why the HINSTANCE you have is not trustworthy, not to say useless...
First of all, an HINSTANCE
is of very little use in modern versions of Windows -- but what you have isn't really an HINSTANCE
anyway. The return from ShellExecute
is really just a value greater than or less than 32, to indicate success or failure respectively.
Fortunately, if you use ShellExecuteEx
, you can get a process handle for the new process, and you can use that to control the process.
The MSDN article that @Remus linked is decent, but (IMO) there's another step that can be useful if the target application is (or might be) a console application. In this case, it usually won't handle a WM_CLOSE
message. You can, however, inject a DLL into the process, and have that do a clean(ish) shutdown from inside the process (for example, if it calls exit
, and the target program is written in C, it'll get a chance to flush and close files, run anything registered with atexit
, etc., before dying).
If that fails, you might want to use GenerateConsoleCtrlEvent
to send it a control-break.
Then, if all those fail to exit you call TerminateProcess
.
精彩评论