Reputation:
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:
LoadLibraryEx
with the arguments should seemed to work (in that GetLastError
returns 0
) 0x43900000
) GetProcAddress
is also odd (0x43902b34
), but reassuringly DLL Export Viewer reports the Init function as having an offset of 0x00002b34
) _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:
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
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