只需要一个可执行文件来枚举进程并枚举已加载的.dll

时间:2013-05-27 07:29:33

标签: winapi dll process proxy

我想做一个带有进程名称stdvector::<std::string>和.dll的std::vector<std::string>的函数来查找它们并将其提供给函数并得到像PROCESSENTRY32 info std::vector<PROCESSENTRY32>返回与名称匹配的任何内容。

你可以谷歌,但不会发现太多,因为我感谢帮助新的winapi,但没有找出事情

1 个答案:

答案 0 :(得分:1)

有一个完美的例子可以完全按照您的要求on MSDN here完成。相关代码复制如下。正如样本的介绍所说

  

要确定哪些进程已加载特定DLL,您必须枚举每个进程的模块。以下示例代码使用EnumProcessModules函数枚举系统中当前进程的模块。

现在示例代码

#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;
}

您需要做的唯一更改是事先push-back std::vector<std::string>感兴趣的模块名称,然后使用枚举模块名称搜索该矢量,而不是打印它们。