调用 GetProcAddress 返回的函数时出现持续访问错误

发布于 2024-10-29 22:24:12 字数 1457 浏览 0 评论 0原文

这是我的代码。这看起来很简单,但不知怎的,它就是行不通。 对函数的最终调用总是失败并出现访问错误。

extern "C"
{
    typedef const char* (*Init_fptr_t)();

    HMODULE CMolNet::LoadDLL()
    {
       string dir = "C:\\MyDllDir\\";
       CA2W dirw( dir.c_str() );
       SetDllDirectory(dirw);

       string dllfile = CombinePath(dir.c_str(), "mydll.dll");
       CA2W dllfilew( dllfile.c_str() );

       mDLL = LoadLibraryEx(dllfilew,0,LOAD_WITH_ALTERED_SEARCH_PATH);
       DWORD err = GetLastError();

       Init_fptr_t iFunc = (Init_fptr_t)GetProcAddress(mDLL,"Init");
       const char *res = (*iFunc)();
    }
}

mydll.dll 是第三方 dll。我没有源代码,但标头中函数的原型如下:

extern "C" {
   const char* Init();
}

mydll.dll 本身依赖于其他几个 dll,存储在目录“C:\MyDllDir”中,因此调用了 SetDllDirectory。

一些观察:

  • 我无法让普通的 LoadLibrary 工作,但是带有参数的 LoadLibraryEx 似乎应该工作(因为 GetLastError 返回 0
  • )返回的 dll 地址似乎很奇怪 (0x43900000)
  • GetProcAddress 返回的函数地址也很奇怪 (0x43902b34),但令人放心的是 DLL Export查看器报告 Init 函数的偏移量为 0x00002b34
  • 调用返回的函数总是会引发访问错误。我已经在函数的 typedef 上尝试了 _ccdecl__stdcall 等的每种组合,但总是得到相同的错误。我尝试过使用和不使用 extern C

其他数据:

  • 这段 C++ 代码是从托管环境中调用的,
  • 我在 Windows 7 64 位上运行,但将非托管部分编译为 win32

我是什么做错了吗?我该如何调试这个?我尝试过 dependency walker 和 dll 导出查看器,一切似乎都正常。

Here is my code. It seems straighforward to do, but somehow it just isn't working.
The final call to the function always fails with an access error.

extern "C"
{
    typedef const char* (*Init_fptr_t)();

    HMODULE CMolNet::LoadDLL()
    {
       string dir = "C:\\MyDllDir\\";
       CA2W dirw( dir.c_str() );
       SetDllDirectory(dirw);

       string dllfile = CombinePath(dir.c_str(), "mydll.dll");
       CA2W dllfilew( dllfile.c_str() );

       mDLL = LoadLibraryEx(dllfilew,0,LOAD_WITH_ALTERED_SEARCH_PATH);
       DWORD err = GetLastError();

       Init_fptr_t iFunc = (Init_fptr_t)GetProcAddress(mDLL,"Init");
       const char *res = (*iFunc)();
    }
}

mydll.dll is a third party dll. I do not have the source code, but the prototype of the function in the header is as follows:

extern "C" {
   const char* Init();
}

mydll.dll itself depends on several other dlls, stored in directory "C:\MyDllDir", hence the call to SetDllDirectory.

Some observations:

  • I could not get vanilla LoadLibrary to work, but LoadLibraryEx with the arguments should seemed to work (in that GetLastError returns 0)
  • The address of the dll returned seems odd (0x43900000)
  • The address of the function returned by GetProcAddress is also odd (0x43902b34), but reassuringly DLL Export Viewer reports the Init function as having an offset of 0x00002b34)
  • Calling the returned function always throws an access errors. I have tried every combination of _ccdecl, __stdcall etc on the typedef for the function but always get the same error. I have tried with and without extern C

Other data:

  • This piece of c++ code is being called from a managed environment
  • I am running on windows 7, 64 bit, but compiling the unmanaged part as win32

What am I doing wrong? How can I debug this? I have tried dependency walker and dll export viewer and everything seems ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2024-11-05 22:24:12

一切都很好。当您通过指针调用函数时,不需要使用 * 。像普通函数一样调用它:

const char *res = iFunc();

而不是

const char *res = (*iFunc)();

Everything is fine. You just don't need to use * when you're calling function through a pointer. Call it like ordinary function:

const char *res = iFunc();

instead of

const char *res = (*iFunc)();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文