开发者

Persistent access error calling a function returned by GetProcAddress

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)
  • T开发者_C百科he 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.


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)();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜