Getting list of running processes

Hi all! I'm trying to get a list of running processes. below is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
         //
    }
    else
    {
        for(int i = 0; i < 1024; i++)
        {
            DWORD processID = aProcesses[i];
            WCHAR szProcessName[50];

            // 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) )
                {
                    GetModuleFileNameExW(hProcess, hMod, szProcessName, cbNeeded);
                }
            }

        }
    }


It's not working but the output shows something like this;


178639408  -  0x22cd70 
177669376  -  0x22cd70 
2280700  -  0x22cd70 
177765861  -  0x22cd70 
2285172  -  0x22cd70 
2007407298  -  0x22cd70 
26734402  -  0x22cd70 
4294967294  -  0x22cd70 
2007787325  -  0x22cd70 


when i want processID - szProcessName. Can someone help me with this please?
Although EnumProcesses() would work, I personally prefer these:

CreateToolhelp32Snapshot(): http://msdn.microsoft.com/en-us/library/ms682489(VS.85).aspx

Process32First(): http://msdn.microsoft.com/en-us/library/ms684834(VS.85).aspx

Process32Next(): http://msdn.microsoft.com/en-us/library/ms684836(VS.85).aspx

DATA_TYPE:
PROCESSENTRY32: http://msdn.microsoft.com/en-us/library/ms684839(VS.85).aspx

They are all in the "Tlhelp32.h" header. These are just so much easier to work with since you only need to kick off the first two then everything else can be stuffed in a loop.

MS Tutorial:
http://msdn.microsoft.com/en-us/library/ms686701(VS.85).aspx

If I remember correctly this one is complete, or else there is very little you actually have to fill in to get it to work.
Last edited on
+1 to Computergeek01's recommendation.

Having said that, your use of EnumProcessModules() doesn't seem 100% right, although it might work (will retrieve only one module handle).

Your use of GetModuleFileNameExW() is incorrect. See http://msdn.microsoft.com/en-us/library/ms683198(VS.85).aspx . The last argument should be sizeof(szProcessName) / sizeof(WCHAR), or if you have _countof(), then _countof(szProcessName). Note, however, that 50 wide characters may not be enough.
Just to add, Open process must be followed by a call to CloseHandle if it succeeds.
http://msdn.microsoft.com/en-us/library/ms684320(VS.85).aspx
When i compile this it's still the same :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;

  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  if( hProcessSnap == INVALID_HANDLE_VALUE )
  {
    printError( TEXT("CreateToolhelp32Snapshot (of processes)") );
    return( FALSE );
  }

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {
    printError( TEXT("Process32First") ); // show cause of failure
    CloseHandle( hProcessSnap );          // clean the snapshot object
    return( FALSE );
  }

  // Now walk the snapshot of processes, and
  // display information about each process in turn
  do
  {
    _tprintf( TEXT("\n\n=====================================================" ));
    _tprintf( TEXT("\nPROCESS NAME:  %s"), pe32.szExeFile );
    _tprintf( TEXT("\n-------------------------------------------------------" ));

    // Retrieve the priority class.
    dwPriorityClass = 0;
    hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
    if( hProcess == NULL )
      printError( TEXT("OpenProcess") );
    else
    {
      dwPriorityClass = GetPriorityClass( hProcess );
      if( !dwPriorityClass )
        printError( TEXT("GetPriorityClass") );
      CloseHandle( hProcess );
    }

    _tprintf( TEXT("\n  Process ID        = 0x%08X"), pe32.th32ProcessID );
    _tprintf( TEXT("\n  Thread count      = %d"),   pe32.cntThreads );
    _tprintf( TEXT("\n  Parent process ID = 0x%08X"), pe32.th32ParentProcessID );
    _tprintf( TEXT("\n  Priority base     = %d"), pe32.pcPriClassBase );
    if( dwPriorityClass )
      _tprintf( TEXT("\n  Priority class    = %d"), dwPriorityClass );

    // List the modules and threads associated with this process

  } while( Process32Next( hProcessSnap, &pe32 ) );

  CloseHandle( hProcessSnap );
  return( TRUE );
(copied from msdn)
it doesnt give the name just the same value 0x22cbb4 which may change abit every new compile. :p
lulul srry for bothering u guys i just had problems with unicode :((
Topic archived. No new replies allowed.