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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, an
HINSTANCEis of very little use in modern versions of Windows — but what you have isn’t really anHINSTANCEanyway. The return fromShellExecuteis 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_CLOSEmessage. 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 callsexit, and the target program is written in C, it’ll get a chance to flush and close files, run anything registered withatexit, etc., before dying).If that fails, you might want to use
GenerateConsoleCtrlEventto send it a control-break.Then, if all those fail to exit you call
TerminateProcess.