即使名称被正确修改,GetProcAddress也会失败

时间:2014-07-25 14:55:52

标签: visual-c++ name-mangling getprocaddress

我在.dll中有以下代码:

namespace MyNamespace
{
    extern "C" __declspec(dllexport) int __stdcall GetOptionID(unsigned long num)
    {
        return 0;
    }
}

这是在Visual C ++ 2010上编译的,所以我还有一个包含GetOptionID的.def文件。我可以看到函数被导出,并使用dumpbin / exports修改为_GetOptionID@4

File Type: DLL

  Section contains the following exports for MyLibrary.dll

    00000000 characteristics
    53D269CB time date stamp Fri Jul 25 15:29:31 2014
        0.00 version
           1 ordinal base
          13 number of functions
          13 number of names

    ordinal hint RVA      name

          1    0 0006F030 CmdOne = _CmdOne@16
          2    1 0006F510 CmdUnimpl = _CmdUnimpl@16
          3    2 0006EBB0 DefineThing = _DefineThing@32
          4    3 0006E0C0 GetOptionID = _GetOptionID@4

在单独的可执行文件中,我尝试检查是否存在GetOptionID

HINSTANCE hinst = LoadLibraryEx(file_name, NULL, DONT_RESOLVE_DLL_REFERENCES);
if(!hinst)
    return FALSE;

FARPROC_IDI lp = (FARPROC_IDI) GetProcAddress(hinst, "_GetOptionID@4");
auto e = GetLastError();

在调试器中运行此代码,我可以看到:

  • LoadLibraryEx成功 - 我有一个看似有效的hinst
  • GetProcAddress失败 - lp0x00000000
  • GetLastError返回127

我可以看到该功能已导出,我可以看到它的名称与我正在寻找的入口点相匹配。 GetProcAddress怎么会失败?

1 个答案:

答案 0 :(得分:0)

啊,自己解决了。在.def文件中定义函数会导致其名称完全无法识别,这意味着GetProcAddress的正确目标只是GetOptionID

但是,由于我有其他.dll进行相同的检查,并且确实是_GetOptionID@4,实际的解决方案是从.def文件中删除GetOptionID。

相关问题