外部“C”没有按预期工作
我正在尝试挂钩 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种损坏是 __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?
您应该使用模块定义文件 (.def),而不是
__declspec(dllexport)
。只需使用以下 .def 文件:
You should use module definition file (.def) instead of
__declspec(dllexport)
.Just use the following .def file:
如果您想执行此操作,则必须按序数导出函数而不是使用 .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.