C++ windows 代码链接在 64 位但不在 32 位

C++ windows code links in 64bit but not in 32bit

我在 windows 上用 C++ 编写了代码。 当我将其编译为 x64 时我的代码会编译和链接,但当我将构建配置更改为 x86 时则不会。

失败是链接错误。

我正在使用 ntdll 中的 RtlIsNameInExpression 函数。

当我在 32 位模式下编译时,出现未解析外部链接错误 (LNK2019)。

知道为什么会发生这种情况吗?

10 倍

该函数的答案在网上documentation

This function has no associated header file. The associated import library, Ntdll.lib, is available in the Microsoft Windows Driver Kit (WDK). You can also call this function using the LoadLibrary and GetProcAddress functions to dynamically link to Ntdll.dll.

如果无法将 WDK 中的 ntdll.lib 文件添加到 link 命令,则需要使用 LoadLibrary-GetProcAddress 方法。

同样来自文档的同一部分:

The functions and structures in Winternl.h are internal to the operating system and subject to change from one release of Windows to the next, and possibly even between service packs for each release. To maintain the compatibility of your application, you should use the equivalent public functions instead. Further information is available in the header file, Winternl.h, and the documentation for each function.

首先 - 您如何声明函数以及找不到链接器的符号?

声明必须

extern "C" NTSYSAPI BOOLEAN NTAPI RtlIsNameInExpression(
                               _In_     PCUNICODE_STRING Expression,
                               _In_     PCUNICODE_STRING Name,
                               _In_     BOOLEAN         IgnoreCase,
                               _In_opt_ PWCH            UpcaseTable
                               );

我猜你错过了 NTAPI__stdacall 关键字,如果你从 here 复制粘贴。对于 x64 只存在一个调用约定,但是对于 x86 在 __stdcall__cdecl 之间存在不同。这可以解释为什么在 x64 中找到但在 x86

中找不到

链接器(不是编译器!)会出现什么错误?未解析的外部符号 __imp__RtlIsNameInExpression ? (如果是,你真的忘记了 __stdcall 设置)或 __imp__RtlIsNameInExpression@16 ?在这种情况下,您声明函数正确,但您的 ntdll.lib 不包含此符号。 (可能是你使用旧的 ntdll.lib 对于 xp? ) 只需搜索 ntdll[p].lib 中的 __imp__RtlIsNameInExpression@16 字符串 - 找到了吗?如果不是,我猜你有旧的 (xp) 版本的 ntdll。