Thread: Process List

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Process List

    To start with, I'm basically looking to printf the current processes (windows) to the console.
    Preferably something like this:

    Code:
    for (int i = 0; i < numOfProcs; i++)
    printf("%s", process)
    I know you have to enumerate the processes somehow, but I couldn't find any existing sources to take a look at.

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    I'm using the following code to do as I wanted but it only shows the names of 32 bit processes. The 64 bit ones return as <unknown>. Oh and the PIDs are printed fine.

    Code:
    #pragma comment(lib, "Psapi.lib") 
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <psapi.h>
    
    void PrintProcessNameAndID( DWORD processID )
    {
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName)/sizeof(TCHAR) );
            }
        }
        _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );
        CloseHandle( hProcess );
    }
    
    int main()
    {
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    	if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) {
            return 0;
    	}
        cProcesses = cbNeeded / sizeof(DWORD);
    	for ( i = 0; i < cProcesses; i++ ) {
    		if( aProcesses[i] != 0 ) {
    			PrintProcessNameAndID( aProcesses[i] );
    		}
    	}
    	getchar();
    	return 0;
    }
    Last edited by david84; 09-06-2010 at 12:48 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A little more investigation on your part, after copying and pasting the MSDN example, would have perhaps led you here, where it states:
    To control whether a 64-bit application enumerates 32-bit modules, 64-bit modules, or both types of modules, use the EnumProcessModulesEx function.
    So you need to modify the MSDN sample code accordingly.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by rags_to_riches View Post
    A little more investigation on your part, after copying and pasting the MSDN example, would have perhaps led you here, where it states:


    So you need to modify the MSDN sample code accordingly.
    Thanks for the response; and I apologize for my lack of reseach. Below is what I have currently.

    I've also tried setting the value of dwFilterFlag to 0x02, LIST_MODULES_64BIT & LIST_MODULES_ALL. None of which have changed the output: it still can only find the 32 bit processes. Are they all invalid values to assign to dwFilterFlag?


    Code:
    #pragma comment(lib, "Psapi.lib") 
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <psapi.h>
    
    void PrintProcessNameAndID( DWORD processID )
    {
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );
    	HANDLE hProc; 
    	DWORD dwFilterFlag = 0x03;
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
            if ( EnumProcessModulesEx( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded, dwFilterFlag) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName)/sizeof(TCHAR) );
            }
        }
    
    	_tprintf( TEXT("%s  (PID: %u).\n"), szProcessName, processID );
        CloseHandle( hProcess );
    }
    
    int main()
    {
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    	if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) {
            return 0;
    	}
        cProcesses = cbNeeded / sizeof(DWORD);
    	for ( i = 0; i < cProcesses; i++ ) {
    		if( aProcesses[i] != 0 ) {
    			PrintProcessNameAndID( aProcesses[i] );
    		}
    	}
    	getchar();
    	return 0;
    }
    Last edited by david84; 09-06-2010 at 05:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM