C++ Win32: Closing process with handle from ShellExecute..

Started by
6 comments, last by iMalc 19 years, 8 months ago
Hi, I'm trying to close an EXE that I run from another program. The first program opens the second one with ShellExecute, and I store the handle: HINSTANCE hserv = ShellExecute(0,"open","server.exe",params,0,SW_SHOWMINNOACTIVE); Then, I wish to close it later. I was wondering what function to use. I have tried a few already, but they just aint' workin :P Thanks for any info! [Edited by - n0ob on August 4, 2004 10:49:44 AM]
Advertisement
Look into CloseHandle and CreateProcess. CreateProcess might be better for you if you are launching child programs from your program.

Easiest way to do close a program would probably be...

SendMessage(handle, WM_CLOSE, 0, 0); // WM_CLOSE / WM_DESTROY
/ ...
Hope that helped

Yeeeahh... ShellExecute returns an HINSTANCE, ... And I really don't want to figure out how to use CreateProcess. I don't see a way to store a handle to what I'm creating.. or whatever.. I obviously don't know enough about that. There has to be a simpler way. THis shouldn't be a hard question people o_^
Also, the program executed at run time is a console application. Maybe if someone told me how to get an HWND handle to a console's window :D
woohoo! I found the function to set the console title!! lol, well, I'll use this for now, it seems to work for me and FindWindow. Tahnks!
CreateProcess returns the process handle in the PROCESS_INFORMATION object you give it. Then you can simply use CloseHandle with that handle to kill the process.
.
You can also use ShellExecuteEx to start the program and get the process handle to it. ShellExecute does not return a handle of any use (other than to test for an error condition).
.
Ihad a similiar problem when I wanted to spawn a console app, then wait until it quits. I gave in and switched from ShellExecute to CreateProcess (as nts suggests) and it all works great now.

I suggest you bite the bullet and do the same.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement