[英]0xC0000005: Access violation executing location 0x00000000
我正在編寫一個MFC項目,嘗試在DLL中調用一個函數,該函數將以字符串形式返回一些信息。 DLL中的功能如下:
int GetInfo(char* Info)
如果成功,該函數將返回0。 信息將在string參數中返回。 調用例程如下:
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);
}
此代碼在編譯和鏈接時沒有錯誤。 執行時,將在上述位置返回“訪問沖突執行位置0x00000000”錯誤。 有人可以建議嗎?
您試圖寫入(或取消引用)NULL指針。 當您檢查調用代碼中所有可能的NULL時,錯誤很可能是在您的調用代碼中。
如果您檢查了您的被調用代碼,但在那也找不到空引用異常的原因,請考慮您可能錯過了正確匹配調用約定的地方。 函數指針的類型應該與庫中的類型完全相同:
對於int GetInfo(char* Info)
,typedef應該為typedef int (*FUNC1)(char* szInfo);
還可能有超過50個字符被寫入緩沖區(szInfo):
char szInfo [50];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.