程序代码片段是:GetProcessNameByPID(DWORD processID, char *pathname)   
{   
    //char szProcessName[MAX_PATH] = "unknown";   
    
    //Get a handle to the process.  
    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);   
    
    //Get the process name.   
    if (hProcess)   
    {   
HMODULE hMod;   
DWORD cbNeeded;   
    
        if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))   
        {   
GetModuleFileNameEx(hProcess, hMod, pathname, MAX_PATH);   
        }   
    }   
    
    //Print the process name and identifier.   
    

    //printf("%s (Process ID: %u)\n", szProcessName, processID);   
    
    CloseHandle(hProcess);   
} 文件的开头,
#include   "psapi.h"
#pragma   comment(lib,"psapi.lib")在32位下可以编译执行。以64位方式编译,提示:error LNK2001: 无法解析的外部符号 _GetModuleFileNameExA在VS2008下编译,用了好几个版本的psapi.lib文件,都是提示同样的错误。有没有人做过64位下根据进程ID获取全路径的,指教一下

解决方案 »

  1.   


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

  2.   

    编译器安装的时候需要选择安装x64的library库
    你可以用编译器安装光盘重新repair添加安装
      

  3.   

    编译通过了。方法:
    perform sdk 2003版安装后,搜索其中的psapi.h和psapi.lib文件,从标示64位的文件夹下把这两个文件复制到项目文件夹里面,编译就通过了。
    看来是原先的lib文件是支持32位的,不能链接,如oyljerry所说。
      

  4.   

    64位下用GetModuleFileName()还是GetModuleFileNameEx()都只能得到32位程序的进程名,得不到64位进程的进程名。你只能用另一个api,GetProcessImageFileName(),但得到的进程路径是以"\\Device\\HarddiskVolume"开头的,要得到进程绝对路径,你还要自己写个这些路径转换的代码才可以。