use*_*698 5 c++ mfc loadlibrary getprocaddress access-violation
我正在编写一个 MFC 项目,该项目尝试调用 DLL 中的函数,该函数将以字符串形式返回一些信息。DLL中的函数如下:
int GetInfo(char* Info)
Run Code Online (Sandbox Code Playgroud)
如果成功,该函数将返回 0。信息将在字符串参数中返回。调用例程如下:
typedef int (WINAPI *FUNC1)(char* szInfo);
HINSTANCE hinstLib;
FUNC1 GetInfo;
char szInfo[50];
hinstLib = LoadLibrary(TEXT("DevInfo.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
GetInfo = (FUNC1) GetProcAddress(hinstLib, "GetInfo");
// If the function address is valid, call the function.
if (NULL != GetInfo)
{
if((GetInfo) (szInfo)) // Error here!!
{
AfxMessageBox(_T("Error Reading"));
}
}
FreeLibrary(hinstLib);
}
Run Code Online (Sandbox Code Playgroud)
此代码在编译和链接时没有错误。执行时,会在上述位置返回“访问冲突执行位置0x00000000”的错误。任何人都可以建议吗?
您尝试写入(或取消引用)NULL 指针。当您检查调用代码中所有可能的 NULL 时,错误很可能出现在您的调用代码中。
如果您检查了调用的代码,但也找不到空引用异常的原因,请考虑您可能错过了正确匹配调用约定。函数指针的类型应该与库中的类型完全相同:
对于int GetInfo(char* Info)typedef 应该是typedef int (*FUNC1)(char* szInfo);