#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    1
    Rep Power
    0

    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

    system("C:\\program.exe");
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    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!
  4. #3
  5. Titles are useless ;)
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Germanski West
    Posts
    216
    Rep Power
    15
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    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.

IMN logo majestic logo threadwatch logo seochat tools logo