Get the PID of a console program

This is a discussion on Get the PID of a console program within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I know how to get the PID of a program that has a window.. I use EnumWindows() , then ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Get the PID of a console program

    Hi, I know how to get the PID of a program that has a window.. I use EnumWindows() , then GetWindowModuleFileName() then GetWindowThreadProcessId() ...
    (I learned it here Commands for terminating programs? )

    But I cant do it with a program that runs in a dos console right ?

    I should create something that returns the PID of a given name.exe program...

    Any suggestion ?
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    cout << GetCurrentProcessId();

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx, but according to http://msdn.microsoft.com/library/de...tprocessid.asp this api seems to return the PID of the currend program... my problem is that I have to look for the PID of another program..

    For example I create the program pid.exe that prints on video the pid of the process fordy.exe
    This forum is the best one I've ever seen. Great ppl, great coders

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Oh...in that case you have a number of options;

    1. CreateToolhelp32Snapshot as given by Ken (Old but good on Win 95/98/ME)
    2. EnumProcesses (Part of PSAPI - Not available on Win 98/95/ME)
    3. WMI - needs Com...so difficult to use in C++

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    ok great, I quite got it

    Another problem now :

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include "psapi.h"
    
    
    void LookforMyProcessAndPID( DWORD processID )
    {
        char szProcessName[MAX_PATH] = "<unknown>";
        char processtofind[MAX_PATH] = "svchost.exe";
    
        // Get a handle to the process.
    
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );
    
        // Get the process name.
    
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
    
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName) );
            }
        }
    
        // Print the process name and identifier.
        
        if (szProcessName == processtofind)
           {
             printf( "%s  (PID: %u)\n", szProcessName, processID );
           }
           
        CloseHandle( hProcess );
    }
    
    main( )
    {
        // Get the list of process identifiers.
    
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;
    
        // Calculate how many process identifiers were returned.
    
        cProcesses = cbNeeded / sizeof(DWORD);
    
        // Print the name and process identifier for each process.
    
        for ( i = 0; i < cProcesses; i++ )
            LookforMyProcessAndPID( aProcesses[i] );
       
       system("PAUSE");
    }
    this code works if I cancel the

    Code:
     if (szProcessName == processtofind)
    if I cancel it, I have the list of all process and PID... but I want to add this control...too bad it seems to fail...
    It looks for szProcessName and looks if it is equal to processtofind

    Code:
    char processtofind[MAX_PATH] = "svchost.exe";
    any suggestion ? I can't figure out why it doesn't work... szProcessName is a char[] and processtofind too.. and szProcessName it is,at least one time in the for cicle in the main, svchost.exe...
    Last edited by BianConiglio; 05-23-2004 at 03:32 PM.
    This forum is the best one I've ever seen. Great ppl, great coders

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    oh god
    I'm a stupid

    I have to do string compare, not " == "

    if (!strcmp(szProcessName, processtofind))

    now it's perfect
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. MSVC++2008 console program portability
    By CodeMonkey in forum Windows Programming
    Replies: 1
    Last Post: 11-18-2008, 03:13 AM
  3. How to avoid console program crash when terminated too early
    By Xargo in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2007, 05:43 PM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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