已得到一个进程名,如svchost.exe或emule.exe之类,如何设置GetModuleFileName第一个参数HMODULE,(除了NULL外不能设置其它?)从而得到对应的可执行文件路径呢?
谢谢各位了!

解决方案 »

  1.   

    第一个参数可以用HMODULE GetModuleHandle(LPCTSTR lpModuleName);的返回值,lpModuleName指向模块的名称 exe 或 dll
    像你说的问题, 可以用 Tool Help Functions来解决
      

  2.   

    使用GetModuleFileNameEx
      

  3.   

    MSDN的Example
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <psapi.h>void 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 list of all the modules in this process.    hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                PROCESS_VM_READ,
                                FALSE, processID );
        if (NULL == hProcess)
            return;    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]);
                }
            }
        }    CloseHandle( hProcess );
    }void main( )
    {
        // Get the list of process identifiers.    DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;    // Calculate how many process identifiers were returned.    cProcesses = cbNeeded / sizeof(DWORD);    // Print the name of the modules for each process.    for ( i = 0; i < cProcesses; i++ )
            PrintModules( aProcesses[i] );
    }
      

  4.   

    GetModuleHandle 或者 LoadLibrary获得HMODULE句柄,而后调用GetModuleFileName HMODULE hModule = ::LoadLibrary("C:\\WINDOWS\\system32\\svchost.exe");
    if(hModule == NULL)
    {
    printf("hModule is NULL!\n");
    }
    else
    {
    TCHAR filename[MAX_PATH];
    if(::GetModuleFileName(hModule, filename, MAX_PATH))
    {
    printf("%s\n", filename);
    }
    }
    ::FreeLibrary(hModule);
      

  5.   

    那个其实是模块加载的基址,就是WinMain或者DllMain函数里第一个参数。
      

  6.   

    不好意思,大家可能误解我了。我想得到的是.exe文件的路径,而不是其模块(.dll)的文件路径,如进程名为QQ.exe,最终得到启动路径C:\ProgramFiles\Tecent\QQ\QQ.exe是这样的又如svchost.exe,-》C:\Windows\system32\svchost.exe之类。。
    请高手支招。谢谢了。