C++ 0xC0000005:访问冲突执行位置0x00000000

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19177822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:34:29  来源:igfitidea点击:

0xC0000005: Access violation executing location 0x00000000

c++mfcaccess-violationloadlibrarygetprocaddress

提问by user2845698

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:

我正在编写一个 MFC 项目,该项目尝试调用 DLL 中的函数,该函数将以字符串形式返回一些信息。DLL中的函数如下:

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:

如果成功,该函数将返回 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);
} 

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?

此代码在编译和链接时没有错误。执行时,会在上述位置返回“访问冲突执行位置0x00000000”的错误。任何人都可以建议吗?

回答by nvoigt

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.

您试图写入(或取消引用)一个 NULL 指针。当您检查调用代码中所有可能的 NULL 时,错误很可能出在被调用代码中。

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);

对于int GetInfo(char* Info)typedef 应该是typedef int (*FUNC1)(char* szInfo);

回答by Jay

It is also possible that more than 50 characters are being written to the buffer (szInfo):

也可能有超过 50 个字符被写入缓冲区 (szInfo):

char szInfo[50];

字符 szInfo[50];