C++: GetProcAddress() fails with local DLL

While trying to retrieve the address of an exported DLL function, I noticed that GetProcAddress() kept failing.

The error code from GetLastError() gave me 127, being ERROR_PROC_NOT_FOUND. Why the hell?

The function being exported is this:

extern __declspec(dllexport) LRESULT CALLBACK BCK_WndRetProc(int nMsg, WPARAM wParam, LPARAM lParam);

So given that information, I was expecting the function name to be "BCK_WndRetProc", because thats the name of the function exported... right?

Quickly checking the exported symbols gave me a surprising result.

1    0 0000100F ?BCK_WndRetProc@@YA_JH_K_J@Z = @ILT+10(?BCK_WndRetProc
@@YA_JH_K_J@Z)

So apparently the exported function name is "?BCK_WndRetProc@@YA_JH_K_J@Z". WTF?

I found out that we're meant to prepend something to the export call.

extern "C" __declspec(dllexport) LRESULT CALLBACK BCK_WndRetProc(int nMsg, WPARAM wParam, LPARAM lParam);

Amazingly, just by adding a little "C" (with quotes) to the export call, the symbol has cleared up quite a bit!

The x86 compiler spat out "_BCK_WndRetProc@12" symbol for the DLL, and the x64 compiler spat out the correct "BCK_WndRetProc" function name.

Weird stuff. I haven't been able to find reasoning behind why this happens, but for curiosity sake any explanations would be appreciated!

 

*edit 11/03/2010*

Thanks Anonymous, I took a look for DLL mangling and found that the "CALLBACK" declaration was using __stdcall, causing the output function name to be all mangled.

[ Extern "C", Cause of DLL mangling ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog