【问题标题】:GCC (MinGW) Linker Fails When Trying to Use GetProcessImageFileNameGCC (MinGW) 链接器在尝试使用 GetProcessImageFileName 时失败
【发布时间】:2015-01-02 16:11:02
【问题描述】:

我正在尝试使用“GetProcessImageFileName”检索远程进程的名称。但是,GCC (MinGW) 链接器失败并出现以下错误:

<source_file>.c: In function '<function_name>':
 warning: implicit declaration of function 'GetProcessImageFileName' [-Wimplicit-function-declaration]
 GetProcessImageFileName(hProcess, szProcessName, MAX_PATH);
 ^
undefined reference to `GetProcessImageFileName'
collect2.exe: error: ld returned 1 exit status

我尝试使用“-lPsapi”和“-lKernel32”进行编译,但得到了相同的结果。 “GetProcessImageFileName”在“Psapi.h”中声明。

我正在使用一台装有 Windows 7 Professional 64 位和 GCC 4.8.1 的机器。有什么想法吗?

【问题讨论】:

  • 我唯一能想到的,没有实际尝试任何东西,就是你将一个“窄”字符串传递给函数的 Unicode 版本。您是否定义了UNICODE,szProcessName 的类型是什么?我猜“是”和char* 或std::string。一些来源会很好。
  • 警告来自编译器,而不是链接器,这意味着 GetProcessImageFileName 尚未声明。所以你需要包含正确的标题。如果函数不在标头中,您可能需要更新版本的 mingw,或者 mingw 本身可能需要更新。
  • @arx Vanilla MinGW 几乎没有 XP 中的任何新功能,例如 GetProcessImageFileName()。如果这就是 XerZetZip 正在使用的,他将需要切换到 mingw-w64。很遗憾,我们今天仍然遇到使用错误 MinGW 的人的问题......
  • @andlabs 干杯,我已将其扩展为答案。
  • @icabod szProcessName 是chars 的数组

标签: c++ c winapi gcc mingw


【解决方案1】:

确保将宏_WIN32_WINNT 定义为larger or equal 0x0501(Windows XP,最低系统支持GetProcessImageFileName)。使用开关-D_WIN32_WINNT=0x0501 或在包含标题之前:

#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <Psapi.h>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    相关资源
    最近更新 更多