简体   繁体   English

外部“ C”无法正常工作

[英]extern “C” not working as expected

I am trying to hook a Win32 API function. 我试图钩住Win32 API函数。 I am making a DLL from which I want to export the function, but I am already failing at the basics. 我正在制作一个要从中导出功能的DLL,但是我已经在基础知识上失败了。 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". 但是导出的函数名称不是“ fnTest”(正如我期望的那样),而是“ _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. 我只能在将函数调用约定声明为__cdecl时使它起作用,这将导致导出的名称为“ fnTest”,但是由于Win32调用WINAPI/__stdcallWINAPI/__stdcall因此这不是一个选择。

I am using VS2010. 我正在使用VS2010。 Thanks in advance. 提前致谢。

That mangling is part of the __stdcall convention. 这种修改是__stdcall约定的一部分。 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? 您能确切解释一下,这怎么引起问题?

You should use module definition file (.def) instead of __declspec(dllexport) . 您应该使用模块定义文件(.def)而不是__declspec(dllexport)
Just use the following .def file: 只需使用以下.def文件:

EXPORTS
fnTest

If you want to do this you will have to export the functions by ordinal rather than by name using a .DEF file. 如果要执行此操作,则必须使用.DEF文件按序而不是按名称导出函数

stdcall provides a decoration that describes the length of the parameters, in this case @0 since you have no parameters. stdcall提供了一种修饰,用于描述参数的长度,在这种情况下为@0因为您没有参数。 If you had one parameter it would be @4 , and so on. 如果您有一个参数,则为@4 ,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM