链接到User32.dll时链接错误2001

mah*_*.cs 5 windows dll linker visual-c++

我正在尝试链接一个目标文件,该文件使用winuser.h中声明的两个方法并在User32.dll中定义:GetMonitorInfo和WindowFromMonitor.源编译到一个目标文件就好了,但是当我尝试链接时,我得到以下错误输出:

D3dCtx.obj : error LNK2001: unresolved external symbol xGetMonitorInfo
D3dCtx.obj : error LNK2001: unresolved external symbol xMonitorFromWindow
Run Code Online (Sandbox Code Playgroud)

问题是,我不称之为"xGetMonitorInfo"或"xMonitorFromWindow".在所有源文件上运行grep表明只调用了"GetMonitorInfo"和"WindowFromMonitor".我正确地包括windows.h,其中包括winuser.h.我也正在链接器选项中正确设置我的LIBPATH,这由详细的链接输出确认.

我的详细链接输出中也出现以下内容:

Found __imp_GetMonitorInfoA
    Referenced in nafxcw.lib(afxribboncategory.obj)
    Referenced in nafxcw.lib(afxtooltipctrl.obj)
    Referenced in nafxcw.lib(afxribbonkeytip.obj)
    Referenced in nafxcw.lib(afxfullscreenimpl.obj)
    Referenced in nafxcw.lib(afxframeimpl.obj)
    Referenced in nafxcw.lib(afxglobalutils.obj)
    Referenced in nafxcw.lib(afxdropdowntoolbar.obj)
    Referenced in nafxcw.lib(wincore.obj)
    Referenced in nafxcw.lib(afxglobals.obj)
    Referenced in nafxcw.lib(afxpopupmenu.obj)
    Referenced in nafxcw.lib(afxpropertygridtooltipctrl.obj)
    Loaded User32.lib(USER32.dll)
Found __imp_MonitorFromWindow
    Referenced in nafxcw.lib(wincore.obj)
    Loaded User32.lib(USER32.dll)
Run Code Online (Sandbox Code Playgroud)

此外,GetMonitorInfo在winuser.h中定义为:

WINUSERAPI
BOOL
WINAPI
GetMonitorInfoA(
    __in HMONITOR hMonitor,
    __inout LPMONITORINFO lpmi);
WINUSERAPI
BOOL
WINAPI
GetMonitorInfoW(
    __in HMONITOR hMonitor,
    __inout LPMONITORINFO lpmi);
#ifdef UNICODE
#define GetMonitorInfo  GetMonitorInfoW
#else
#define GetMonitorInfo  GetMonitorInfoA
#endif // !UNICODE
Run Code Online (Sandbox Code Playgroud)

当我将对"GetMonitorInfo"的所有引用更改为"GetMonitorInfoA"时,我只会得到

D3dCtx.obj:错误LNK2001:未解析的外部符号xMonitorFromWindow

作为我的链接器错误输出.不幸的是,MonitorFromWindow似乎没有多个版本可用.

我应该注意到我使用的是64位版本的库,链接和cl.

这里发生了什么,我怎样才能成功链接我的程序?

Aam*_*mir 3

我不知道您是否能够找到解决方案,但我遇到了同样的问题,发生这种情况的原因是我包含一个名为multimon.h

看起来在 64 位编译的情况下,由于宏定义,这些函数的定义来自两个源,并且可能其中一个源multimon.h是覆盖的并且是错误的。

我通过注释掉这个包含来解决它,并且它已经开始正常链接。

//#include <multimon.h>
Run Code Online (Sandbox Code Playgroud)