dllloader
dllloader

Reputation: 11

GetProcAddress doesn't work for functions other than void

I have a problem with GetProcAddress: I wrote a simple DLL with just one function in it:

extern "C" LRESULT WINAPI Function(HWND Hwnd, UINT Message,
                                   WPARAM wParam, LPARAM lParam)
{
    Beep(1000, 1000);
    return CallNextHookEx(0, Message, wParam, lParam);
}

When I try to get function's address GetProcAddress fails with the ErrorCode 127 (ERROR_PROC_NOT_FOUND). However, if I use void as the function type it works perfectly. I can't really figure out why it behaves like this. Any suggestions would be greatly appreciated!

BTW: DependencyWalker shows that the function's name is indeed "Function" no changes have been applied.

Upvotes: 1

Views: 3547

Answers (2)

Jakub C
Jakub C

Reputation: 615

It's good idea to use module definition (.def file) with names of exported functions instead of __declspec(dllexport). It's much easier to manage them.

Also this

#define DllExport extern "C" __declspec (dllexport)

causes that exported dll function names are without any c++ "decorations"

Upvotes: 2

Hans Passant
Hans Passant

Reputation: 941218

There are only two failure modes for GetProcAddress:

  • you didn't export the function
  • you didn't get the name right

The exported named of this function is not "Function" unless you used a .def file to rename the export or created a 64-bit DLL. It will be "_Function@16" for a 32-bit build. The @16 postfix would be strongly associated with the fact that you have trouble making it work for functions with any arguments.

From the Visual Studio Command Prompt run Dumpbin.exe /exports on your DLL to see the exports. Delete a .pdb file in the same directory if there is one.

Upvotes: 4

Related Questions