开发者

GetProcAddress doesn't work for functions other than void

开发者 https://www.devze.com 2023-03-17 01:08 出处:网络
开发者_开发百科I have a problem with GetProcAddress: I wrote a simple DLL with just one function in it:
开发者_开发百科

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.


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.


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"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号