Thread: Stopping a process

  1. #1
    george7378
    Guest

    Stopping a process

    Hi everyone,

    I have a program which uses WinExec to start some external processes when I press a button. I was wondering what I would use to kill an external process by pressing a button in the same program (for example, when I press 'close' on my program, how could I make it kill processes X, Y and Z as well as itself?).

    Thanks!

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    If you want an elegant way to manage another process, it is not as trivial as WinExec:
    Process and Thread Functions (Windows)

  3. #3
    george7378
    Guest
    Thanks - I have been looking around for days based on the stuff in there, but I can't find anything. Would it be possible to give me a little example for how to close a process when a button is pressed (i.e.

    Code:
    case IDC_BUTTON:
    //Code to close desired processes
    break;
    I just want to close other specific open programs when I run another one.

    Thanks.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Note to mods: this is a win32 programming question, not a C++ question.

    Assuming you have a handle to the process, it is as simple as TerminateProcess(handle, exitcode) where exitcode is an integer value to be returned. That is not usually a good idea if the program has windows (i.e. a GUI) though. In that case, it is often better to send appropriate messages to the main window of the application in order to tell it to close down cleanly.

    If you use CreateProcess() to create the process in the first place, one of the bits of information CreateProcess() gives back to the caller is a handle to the created process. You can also use EnumProcesses() to search through available processes, and find one with required characteristics.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    george7378
    Guest
    I've been looking at EnumProcesses, but I'm a little confused with what I have to do - I suppose it's something like this:

    Code:
    EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded )
    ...but I don't know what to do after that! Is it quick to make an example?

    Thanks.

  6. #6
    george7378
    Guest
    I have changed it so that I use ShellExecute to start the processes - could anyone provide me with an example of how to close them by pressing a Win32 button?

    Thanks.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    ShellExecute does not return a handle to the new process, so does not provide enough information to allow your code to directly shut down that process. You will therefore need to find your process using EnumProcesses().

    Preceding posts have given pointers on how to proceed. You really need to read the documentation from there. This link provides an example of usage of EnumProcesses() for printing out information about processes. All you need to do is change the PrintProcessNameAndID() function to something like DetectTheProcessToTerminateAndThenTerminateIt(). To do that, you need to identify some characteristic the process has that distinguishes it from other processes. The decision of how you do that is yours.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stopping Winform Process
    By darren78 in forum C# Programming
    Replies: 2
    Last Post: 10-16-2010, 10:55 AM
  2. how to get process info ( to extract process thread id )
    By umen242 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2009, 01:08 PM
  3. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM
  4. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM
  5. Stopping pop-ups...
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-13-2002, 07:24 AM