user236520
user236520

Reputation:

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:

Other data:

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

Upvotes: 0

Views: 405

Answers (1)

absunder
absunder

Reputation: 11

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

Upvotes: 1

Related Questions