EnumProcesses (psapi.h)

EnumProcesses returns a list of process ID's. We can then use OpenProcess to obtain a handle on the processes for further enumeration.

EnumProcesses is a quick and easy way to get all process ID's on a Windows system. It's a good idea to use a large array because it's hard to predict how many processes there will be at the time you call EnumProcesses.

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>

int main( void )
{
    // Get the list of process identifiers.

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

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }


    // 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++ )
    {
        if( aProcesses[i] != 0 )
        {
            // calls OpenProcess to get handle
            PrintProcessNameAndID( aProcesses[i] );
        }
    }

    return 0;
}

Last updated