我如何明确指定 MSVC 编译器/链接器链接到 API sets (*ms-win* 模式匹配 dll)而不是 kernel32.dll、ntdll.dll 等?
例如,我创建了一个简单的 dll,它只调用一些基本的 WinAPI。检查它的 IAT(通过 CFF Explorer、python pefile 库等)时,只有 kernel32.dll。 我希望看到的是 *ms-win* dll。
最佳答案
API 集在内部使用,这意味着即使是 Windows 7、8 或 10 上的“遗留”系统 DLL 也会使用它们。因此,您没有理由需要直接使用它们。
也就是说,您可以使用许多“伞式”库,而不是链接到经典的 KERNEL32.DLL
等:
- onecore.lib
- onecoreuap.lib
- onecore_apiset.lib
请记住,这些旨在匹配与它们随附的 Windows 10 SDK 相同的 Windows 构建(即它们向前兼容,而不是向后兼容)。 *_downlevel.lib
版本也支持旧版本的 Windows。这些主要供驱动程序开发人员使用 Microsoft Docs
UWP 应用使用它们自己的伞形库 WindowsApps.lib
/WindowsApps_downlevel.lib
。
参见 Microsoft Docs有关umbrella 库的更多信息。
Note that you should not link with more than one umbrella library, and you shouldn't mix kernel32.lib with umbrella libraries in the same link. This is actually a bit challenging to do with MSBuild default rules for scenarios other than
WindowsApps.lib
. There's an improvement coming in VS 2022 17.2 that addresses this. See this VS feedback issue.
Also due to a quirk of the Microsoft Visual C/C++ Runtime, if you are linking with
onecore_apiset.lib
, you should use/IGNOREDEFAULTLIB:onecore.lib /IGNOREDEFAULTLIB:kernel32.lib
as well. It's also a good idea to use/SUBSYSTEM:WINDOWS,10.0
or/SUBSYSTEM:CONSOLE,10.0
. See this VS feedback issue.
关于c++ - windows - 如何链接 API 集 (*ms-win*) 而不是 kernel32.dll、ntdll.dll 等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65662707/