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

查看:140
本文介绍了为什么我在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.

推荐答案

据我所知,这种方法将无法在代码为64位时检索64位进程信息。在32位进程中执行。在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

  1. Use a separate 64 bit process, and some IPC, to retrieve the information.
  2. Use WMI to get the module file name.
  3. Use 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天全站免登陆