加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

win7(64位)下,无法通过OpenProcess和GetModuleFileNameEx得到进程的全路径

(2012-08-09 02:48:25)
标签:

杂谈

分类: windows
CreateToolhelp32Snapshot不适用64位进城。
MSDN原文:

th32ProcessID 
If the specified process is 64-bit process and the caller is 32-bit process, this function fails and the last error code is ERROR_PARTIAL_COPY (299).

用EnumProcesses取得进城ID,然后调用上面的GetProcessPath(...)

以下是EnumProcesses的 MSDN Sample Code,很简单,翻译一下就行。

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

void PrintProcessNameAndID( DWORD processID )
{
    TCHAR szProcessName[MAX_PATH] TEXT("<unknown>");

    // Get handle to the process.

    HANDLE hProcess OpenProcess( PROCESS_QUERY_INFORMATION |
          PROCESS_VM_READ,
          FALSE, processID );

    // Get the process name.

    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
          &cbNeeded) )
        {
          GetModuleBaseName( hProcess, hMod, szProcessName, 
          sizeof(szProcessName)/sizeof(TCHAR) );
        }
    }

    // Print the process name and identifier.

    _tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );

    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 and process identifier for each process.

    for 0; cProcesses; i++ )
        if( aProcesses[i] != )
          PrintProcessNameAndID( aProcesses[i] );
}
---------------------------------------------

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有