February 7th, 2003, 04:00 PM
How to close a another program within C++
I'm using the system call to run a program within my code but I want to be able to close that program after a few seconds. What happens is I call the prgram and it runs and takes over my screen but it's waiting for the user to hit the ESC key to exit the program so if I don't hit exit...it'll stay on that program screen and won't return control to my program. What I was wondering if there is a way to kill that program after it runs for a while.
I'm creating a console project and I used this to call the other program
February 7th, 2003, 04:03 PM
If you have the handle to the program, you can try sending it a WM_QUIT message using SendMessage(). If you're executing the program using system, instead of CreateProcess, then you can attempt to get the program's handle by using FindWindow() (assuming you know the title of the window or its window class).
Hope this helps!
February 10th, 2003, 09:43 AM
You can also use TerminateProcess!
But this is very unusual.
If you really want to "Shoot down" the program use it.
With the SendMessage program will behave as if user clicked x-icon.
WIth TerminateProcess it ends immediatly
February 10th, 2003, 01:24 PM
TerminateProcess() would shutdown the program quite unclean. eg. it leave dlls open in memory. Avoid it if you can. But for a hanging program, it is often the only way.