外部“C”没有按预期工作

发布于 2024-10-02 10:24:33 字数 320 浏览 0 评论 0原文

我正在尝试挂钩 Win32 API 函数。我正在制作一个 DLL,我想从中导出该函数,但我在基础知识上已经失败了。我的声明如下:

extern "C" __declspec(dllexport) int WINAPI fnTest(void);

但导出的函数名称不是“fnTest” - 正如我所期望的 - 而是“_fnTest@0”。我只能在将函数调用约定声明为 __cdecl 时使其工作,这会导致导出名称“fnTest”,但由于 Win32 调用连接是 WINAPI/__stdcall这不是一个选择。

我用的是VS2010。提前致谢。

I am trying to hook a Win32 API function. I am making a DLL from which I want to export the function, but I am already failing at the basics. My declaration is as follows:

extern "C" __declspec(dllexport) int WINAPI fnTest(void);

but the exported function name is not "fnTest" - as I would expect - but is "_fnTest@0". I can only make it work when declaring the functions calling convention to __cdecl, which results to an exported name of "fnTest", but since the Win32 calling conection is WINAPI/__stdcall this is not an option.

I am using VS2010. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭了丶谁疼 2024-10-09 10:24:33

这种损坏是 __stdcall 约定的一部分。由于被调用函数有责任在返回时从堆栈中删除参数,并且从堆栈中删除错误数量的数据会产生灾难性的后果,因此参数所占用的字节数只需附加到函数名称的“@”后面即可让链接器捕获潜在的冲突定义错误。

您能具体解释一下,这会造成什么问题吗?

That mangling is part of the __stdcall convention. As the called function has the responsibility to remove the parameters from the stack on return, and removing the wrong amount of data from the stack has disastrous consequences, the number of bytes the parameters take is simply appended to the function name after "@" to let the linker catch potential conflicting definition errors.

Could you explain exactly, how does this pose a problem?

泅渡 2024-10-09 10:24:33

您应该使用模块定义文件 (.def),而不是 __declspec(dllexport)
只需使用以下 .def 文件:

EXPORTS
fnTest

You should use module definition file (.def) instead of __declspec(dllexport).
Just use the following .def file:

EXPORTS
fnTest
幸福%小乖 2024-10-09 10:24:33

如果您想执行此操作,则必须按序数导出函数而不是使用 .DEF 文件按名称。

stdcall 提供了一个描述参数长度的装饰,在本例中是 @0 因为您没有参数。如果您有一个参数,它将是 @4,依此类推。

If you want to do this you will have to export the functions by ordinal rather than by name using a .DEF file.

stdcall provides a decoration that describes the length of the parameters, in this case @0 since you have no parameters. If you had one parameter it would be @4, and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文