Auflisten aller Module für einen Prozess

Um zu bestimmen, welche Prozesse eine bestimmte DLL geladen haben, müssen Sie die Module für jeden Prozess auflisten. Im folgenden Beispielcode wird die EnumProcessModules-Funktion verwendet, um die Module der aktuellen Prozesse im System aufzulisten.

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

// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1

int PrintModules( DWORD processID )
{
    HMODULE hMods[1024];
    HANDLE hProcess;
    DWORD cbNeeded;
    unsigned int i;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Get a handle to the process.

    hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                            PROCESS_VM_READ,
                            FALSE, processID );
    if (NULL == hProcess)
        return 1;

   // Get a list of all the modules in this process.

    if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
    {
        for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
        {
            TCHAR szModName[MAX_PATH];

            // Get the full path to the module's file.

            if ( GetModuleFileNameEx( hProcess, hMods[i], szModName,
                                      sizeof(szModName) / sizeof(TCHAR)))
            {
                // Print the module name and handle value.

                _tprintf( TEXT("\t%s (0x%08X)\n"), szModName, hMods[i] );
            }
        }
    }
    
    // Release the handle to the process.

    CloseHandle( hProcess );

    return 0;
}

int main( void )
{

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

    // Get the list of process identifiers.

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

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the names of the modules for each process.

    for ( i = 0; i < cProcesses; i++ )
    {
        PrintModules( aProcesses[i] );
    }

    return 0;
}

Die Standard-Funktion ruft mithilfe der EnumProcesses-Funktion eine Liste von Prozessen ab. Für jeden Prozess ruft die Standard-Funktion die PrintModules-Funktion auf und übergibt ihr den Prozessbezeichner. PrintModules wiederum ruft die OpenProcess-Funktion auf, um das Prozesshandle abzurufen. Wenn OpenProcess fehlschlägt, wird in der Ausgabe nur der Prozessbezeichner angezeigt. Beispielsweise schlägt OpenProcess für die Prozesse Im Leerlauf und CSRSS fehl, da deren Zugriffseinschränkungen verhindern, dass Code auf Benutzerebene sie öffnet. Als Nächstes ruft PrintModules die EnumProcessModules-Funktion auf, um die Handles-Funktion des Moduls abzurufen. Schließlich ruft PrintModules einmal für jedes Modul die GetModuleFileNameEx-Funktion auf, um die Modulnamen abzurufen.