I'm writing an MFC project that try to call a function in the DLL which will return some information in a string. The function in the DLL is as follows:
int GetInfo(char* Info)
The function will return 0 if success. Information will be returned in the string parameter. The calling routine is as follows:
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);
}
This code does not have error in compiling and linking. When executing, it will return the error of "Access violation executing location 0x00000000" at the location stated above. Can anyone advice?
You tried to write to (or dereference) a NULL pointer. As you checked all possible NULLs in your calling code, the error most likely is in your called code.
If you checked your called code and cannot find a reason for a null reference exception there either, consider that you may have missed to match the calling conventions correctly. The type of your function pointer should be EXACTLY the same as in your library:
For int GetInfo(char* Info)
the typedef should be typedef int (*FUNC1)(char* szInfo);