Closing other programs. (C++)

This is a discussion on Closing other programs. (C++) within the Windows Programming forums, part of the Platform Specific Boards category; How can I close another running program by reference of the file (.exe) name?...

  1. #1
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293

    Closing other programs. (C++)

    How can I close another running program by reference of the file (.exe) name?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Use EnumProcesses() to get a list of all the running processes. Then use OpenProcess() on each of them to get handles. Once you've got the handle you can use GetModuleFileName() to see if that process matches your filename, and if it does, use something like TerminateProcess() to close it.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    I'll have a go at that. Thankyou very much.

  4. #4
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    I can't do it. I don't even know the syntax of EnumProcesses(), and google didn't find me anything I could understand.

    If it's not to much trouble do you think you could show me how to list the current processes?

  5. #5
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    MSDN
    Do not make direct eye contact with me.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Okey dokes, It's a compiler problem I have now.

    I'm using dev-c++ and I'm getting this error:

    [Linker error] undefined reference to `EnumProcesses@12'

    My code it this:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <psapi.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      DWORD aProcesses[1024],cbNeeded;
      EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded );
      system("PAUSE");	
      return 0;
    }
    Thanks for the help so far.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you see this example ? Does it help you?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Yup. I guess it's just the compiler. Looks like I'll have to strengthen the Gates Empire more by buying Visual C++, which was the last thing I wanted to do because I don't have a job and it'll take my life savings to get a copy (I'm only 16).

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> I guess it's just the compiler.

    From memory, those headers and libraries are not in the standard compiler stuff, they are included with the Platform SDK. Have you downloaded that?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    What's that and where can I get it? I've seen it metioned in many places and I guess it stands for software dev. kit and that it probably aids in developing for win32 platform but that's about all I guess..

    If you could please elaborate I'd be very grateful.

  12. #12
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I just killed my SDK and tried to compile this...
    Code:
    #include <windows.h>
    #include <psapi.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        DWORD Pids[100];
        DWORD BytesUsed;
        int RetVal;
    
        RetVal = EnumProcesses(Pids,
                               sizeof(Pids),
                               &BytesUsed);
        cout << "There are " << (BytesUsed/sizeof(DWORD)) << " processes." << endl;
    
        return 1;
    }
    ... and got exactly the same error as you. Reinstall the SDK, (actually re-link it to VC), and the thing compiles and runs fine, so I would definitely try this first.

    There are a few different SDK's some are only interesting if you have other bits of software, look here. Warning! Some of these downloads are very big!

    *** EDIT ***

    Your guesses as to what it is and what the SDK stand for are correct.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  13. #13
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Okey dokey, since I'm on a 56k and I'm almost over my 500mb download limit for the month (can't get broadband where I live ) I have to wait a few days to start the download and then probably a day for it to download.

    I'll be back sometime in the next few days.

    Thanks for all your help so far.

  14. #14
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    The other routines I mentioned are in the standard windows.h, so you should be able to try them out, and maybe write a program which allows you to manually enter a PID. That way, when you've got the SDK you'll only need to stick that bit on the front.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  15. #15
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    The PSAPI stuff isnt accessable easilly with the standard includes and libs that comne with VC++, you need the SDK (as adrian said).

    I have been caught needing these funcs before on a clean install of my compiler (same problem as you - a 100MB+ download on 56K is a swine!), so if your in a tight place you can manually do the loading yourself...

    Here's an example I used of getting EnumProcesses without the SDK - EnumWindows + XP


    Might give you an option for now until you have downloaded the SDK (PS....I have broadband now - definately worth the upgrade )

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  2. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  3. Programs opening programs
    By LinuxPLC in forum C Programming
    Replies: 1
    Last Post: 11-21-2002, 12:50 PM
  4. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 04:12 AM
  5. Closing Programs?
    By BubbleMan in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2001, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21