gpt4 book ai didi

How to create an import library for kernel32.dll using a .def file on x86?(如何在x86上使用.def文件为kernel32.dll创建导入库?)

转载 作者:bug小助手 更新时间:2023-10-24 21:24:48 28 4
gpt4 key购买 nike



I have a small project that does not depend on the CRT or windows sdk. In order to link against kernel32.dll I created a minimal .def file with only the couple functions I need:

我有一个不依赖于CRT或Windows SDK的小项目。为了链接到kernel32.dll,我创建了一个最小的.def文件,其中只包含我需要的几个函数:


LIBRARY kernel32.dll
EXPORTS
ExitProcess

I then create the .lib during the build with this command:
lib /DEF:kernel32.def /OUT:kernel32.lib /MACHINE:x86.

然后,我在构建过程中使用以下命令创建.lib:lib/def:kernel32.def/out:kernel32.lib/Machine:x86。


The declaration of the function in my code looks like this:

我的代码中的函数声明如下所示:


#ifdef __cplusplus
extern "C"{
#endif
__declspec(dllimport) __declspec(noreturn) void __stdcall ExitProcess(UINT uExitCode);
#ifdef __cplusplus
}
#endif

This works perfectly fine when compiling and linking for x64 (with a a lib created with /MACHINE:x64) but when I switch over to x86 I get an unresolved symbol error for __imp__ExitProcess@4.

这在编译和链接x64(使用/Machine:x64创建的库)时工作得很好,但当我切换到x86时,我收到__imp__ExitProcess@4的一个未解决的符号错误。


This linker error is resolved by changing the name of the export in the def file to ExitProcesss@4 but when running the program, an error appears saying The procedure entry point 'ExitProcess@4' was not found in MyProgram.exe.

通过将def文件中的导出名称更改为ExitProcesss@4可以解决此链接器错误,但在运行该程序时,会出现一个错误,指出在MyProgram.exe中找不到过程入口点‘ExitProcess@4’。


Everything works fine if I link against the kernel32.lib from the windows sdk. So the issue must be somewhere between creating the def file and creating an import library.

如果我从Windows SDK链接到kernel32.lib,一切工作正常。因此,问题肯定介于创建def文件和创建导入库之间。


The only reference I could find was a .def file from mingw which looks exactly like mine: https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/lib32/kernel32.def
Although since this file is probably consumed by mingw tools it might have a different output than the lib tool from msvc.

我能找到的唯一引用是来自mingw的.def文件,它看起来与我的一模一样:https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/lib32/kernel32.def,尽管由于该文件可能被mingw工具使用,它的输出可能与msvc中的lib工具不同。


更多回答
优秀答案推荐

have only def file is not enouth in case x86 here.

在x86的情况下,只有def文件是不够的。


if will be name ExitProcess in def - will be exactly __imp_ExitProcess in lib too. but client wait for __imp__ExitProcess@4. so you fail at build time.

如果将在def中命名为ExitProcess,则在lib中也将恰好是__imp_ExitProcess。但是客户端等待__imp__ExitProcess@4。所以您在构建时失败。


if you set ExitProcess@4 your PE will be import _ExitProcess@4 from kernel32.dll and fail at runtime

如果设置ExitProcess@4,则PE将从kernel32.dll导入_ExitProcess@4,并在运行时失败


solution here use c/cpp/asm file in conjunction with def

此处解决方案将c/cpp/ASM文件与def结合使用


minimal solution: first use such kernel32.asm

最小解决方案:首先使用这样的内核32.asm


.686
.model flat

.code

_ExitProcess@4 proc
_ExitProcess@4 endp

end

and compile it as ml /c /Cp kernel32.asm

并将其编译为ml/c/Cp kernel32.asm


def is simply

Def是简单的


EXPORTS
ExitProcess

and finally use next command

最后使用Next命令


link /DLL /NOENTRY /MACHINE:x86 /def:kernel32.def kernel32.obj 

after this you got correct kernel32.lib ( kernel32.dll and kernel32.exp simple for delete)

之后,您获得了正确的kernel32.lib(kernel32.dll和kernel32.exp,用于删除)


更多回答

Thank you, that works. Could you maybe explain why you need to do it that way for x86? I would assume it should be possible to create an import library from a def file alone.

谢谢你,这很管用。您能解释一下为什么x86需要这样做吗?我假设应该可以仅从def文件创建导入库。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com