为什么我在 64 位 Windows 8 上从 GetModuleFileNameEx 得到废话? [英] Why do I get nonsense from GetModuleFileNameEx on 64-bit Windows 8?

查看:26
本文介绍了为什么我在 64 位 Windows 8 上从 GetModuleFileNameEx 得到废话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个

function NazwaProcesu(const uchwyt: Thandle): string;
var
  pid: DWORD;
  hProcess: Thandle;
  sciezka: array [0..MAX_PATH - 1] of char;
begin
  GetWindowThreadProcessId(uchwyt, pid);
  hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, FALSE, pid);
  if hProcess <> 0 then
    try
      GetModuleFileNameEx(hProcess, 0, sciezka, MAX_PATH)
    finally
      Result := sciezka;
      CloseHandle(hProcess);
    end;
end;

在 Windows 7 32 位上没有问题.在 Win 8 x64 上,我有这个:

On windows 7 32 bit no problems. On Win 8 x64 i have this:

我猜最后 3 个条目应该是 explorer.exe.感谢您的帮助.

where last 3 entries should be explorer.exe, i guess. Thanks for help.

推荐答案

据我所知,当代码在 32 位进程中执行时,这种方法将无法检索 64 位进程信息.在 WOW64 模拟器内部运行时,此类问题很常见,避免此类问题的最简洁方法是在 WOW64 模拟器外部运行代码.也就是说,在 64 位进程中运行您的代码.

As far as I am aware, this approach will fail to retrieve 64 bit process information when the code is executed in a 32 bit process. This sort of problem is common when running inside the WOW64 emulator and the cleanest way to avoid such issues is to run the code outside of the WOW64 emulator. That is, run your code in a 64 bit process.

如果您无法重新编译为 64 位,其他可能的解决方法是:

Other possible ways to work around this, if you cannot re-compile as 64 bit are:

  1. 使用单独的 64 位进程和一些 IPC 来检索信息.
  2. 使用 WMI 获取模块文件名.
  3. 使用QueryFullProcessImageName.

我总是喜欢强调的另一点是错误检查很重要.您在调用 GetModuleFileNameEx 时未能检查错误.调用显然失败了,这就是为什么你最终得到一个未初始化的文本缓冲区.始终检查 Windows API 调用是否有错误.

Another point that I always like to stress, is that error checking is important. You fail to check for errors when you call GetModuleFileNameEx. The calls are clearly failing which is why you end up with an uninitialized text buffer. Always check Windows API calls for errors.

这篇关于为什么我在 64 位 Windows 8 上从 GetModuleFileNameEx 得到废话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆