Murdock
Murdock

Reputation: 1

extern "C" not working as expected

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.

Upvotes: 0

Views: 3404

Answers (3)

slacker
slacker

Reputation: 2142

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?

Upvotes: 3

Abyx
Abyx

Reputation: 12928

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

EXPORTS
fnTest

Upvotes: 1

Steve Townsend
Steve Townsend

Reputation: 54178

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.

Upvotes: 0

Related Questions