1

我想制作一个带有stdvector::<std::string>进程名称和std::vector<std::string>.dll 参数的函数,以便在其中找到并将其提供给函数,并获得std::vector<PROCESSENTRY32>与名称匹配的任何返回的 PROCESSENTRY32 信息。

你可以用谷歌搜索,但找不到很多东西

4

1 回答 1

1

有一个完美的例子可以在 MSDN 上完全按照您的要求进行操作。相关代码复制如下。正如样品介绍所说

要确定哪些进程加载了特定的 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>,然后使用枚举的模块名称搜索该向量,而不是打印它们。

于 2013-05-27T08:31:55.723 回答