调用 GetProcAddress 返回的函数时出现持续访问错误
这是我的代码。这看起来很简单,但不知怎的,它就是行不通。 对函数的最终调用总是失败并出现访问错误。
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 thatGetLastError
returns0
) - 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 of0x00002b34
) - Calling the returned function always throws an access errors. I have tried every combination of
_ccdecl
,__stdcall
etc on thetypedef
for the function but always get the same error. I have tried with and withoutextern 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一切都很好。当您通过指针调用函数时,不需要使用
*
。像普通函数一样调用它:而不是
Everything is fine. You just don't need to use
*
when you're calling function through a pointer. Call it like ordinary function:instead of