在 "<name>.exe" 中0x00007FFF168E1657 (vcruntime140d.dll) 引发异常:0xC0000005:访问冲突写入位置0x0000000000000

Exception thrown at 0x00007FFF168E1657 (vcruntime140d.dll) in "<name>.exe": 0xC0000005: Access violation writing location 0x0000000000000000

本文关键字:0xC0000005 访问冲突 异常 0x0000000000000 位置 vcruntime140d exe gt name lt 0x00007FFF168E1657      更新时间:2023-10-16

我尝试为进程间通信(IPC(创建两个不同的Visual c ++控制台应用程序。两个代码的生成都成功。但是,当我尝试调试它时,我收到这样的异常"在FileMapServer_Parent.exe中0x00007FFF168E1657(vcruntime140d.dll抛出异常:0xC0000005:访问违规写入位置0x0000000000000000"

//PARENT PROCESS:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <Tlhelp32.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <cstring>
#include <cctype>
#include <fstream>
using namespace std
int main()
{
cout << "tt.....FILEMAPPING SERVER or PARENT....." << endl;
cout << endl;
//Local Variable Definitions

    HANDLE  hFileMap;
    BOOL    bResult;
    PCHAR   lpBuffer = NULL;
    char    Buffer[256] = "Hello From File Map Server";
    size_t  szBuffer = size(Buffer);

// STEP 1 : Create File Map
        hFileMap = CreateFileMapping(
            INVALID_HANDLE_VALUE,
            NULL,
            PAGE_READWRITE,
            0,
            256,
            L"LOCAL\MyFileMap);

if (hFileMap == FALSE)
{
    cout << "CreateFileMapping Failed & Error Number - " << GetLastError() << endl;
}
else
    cout << "CreateFileMapping Success - " <<  endl;

// STEP 2 : Map View of File
lpBuffer = (PCHAR)MapViewOfFile(
    hFileMap,
    FILE_MAP_ALL_ACCESS,
    0,
    0,
    256);
if (lpBuffer == NULL)
{
    cout << "MapViewOf File Failes & Error No - " << GetLastError() << endl;
}
else
    cout << "MapViewOf File Success "  << endl;

//STEP 3 : Copy Memory Function
CopyMemory(lpBuffer,Buffer,szBuffer);

//STEP 4 : Unmap View Of File
bResult = UnmapViewOfFile(lpBuffer);
if (bResult == FALSE)
{
    cout << "UnMapViewOfFile Failed & Error No - " << GetLastError() << endl;
}
else
    cout << "UnMapViewOfFile FSuccess - " << endl;
system("PAUSE");
return 0;
}

运行父进程时出现异常: [1]: https://i.stack.imgur.com/bomQB.png

//CHILD PREOCESS:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <Tlhelp32.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <cstring>
#include <cctype>
#include <fstream>
using namespace std;
int main()
{
cout << "tt.....FILEMAPPING CLIENT or CHILD....." << endl;
cout << endl;
// Local Variable Definitions
HANDLE  hFileMap;
BOOL    bResult;
PCHAR   lpBuffer = NULL;
// STEP 1 : OpenFileMapping
hFileMap = OpenFileMapping(
    FILE_MAP_ALL_ACCESS,
    FALSE,
    L"LOCAL\MyFileMap");
if(hFileMap == NULL)
{
    cout << "OpenFileMap Failed & error - " << GetLastError() << endl;
}
else
    cout << "OpenFileMap success " << endl;

//STEP 2 : MapViewOfFile
lpBuffer = (PCHAR)MapViewOfFile(
    hFileMap,
    FILE_MAP_ALL_ACCESS,
    0,
    0,
    256);
    if (lpBuffer == NULL)
    {
        cout << "MapViewOf File Failes & Error No - " << GetLastError() << endl;
    }
    else
        cout << "MapViewOf File Success " << endl;
//STEP 3 : Reading the data from File Map Object
    cout << "DATA READING FROM PARENT PROCESS--->" <<lpBuffer<< endl;
//STEP 4 : UnMapViewOfFile
    bResult = UnmapViewOfFile(lpBuffer);
    if (bResult == FALSE)
    {
        cout << "UnMapViewOfFile Failed & Error No - " << GetLastError() << endl;
    }
    else
        cout << "UnMapViewOfFile FSuccess - " << endl;
//STEP 5 : Close Handle
    CloseHandle(hFileMap);
system("PAUSE");
return 0;
}

问题就在这里:

hFileMap = CreateFileMapping(
    INVALID_HANDLE_VALUE,
    NULL,
    PAGE_READWRITE,
    0,
    256,
    L"LOCAL\MyFileMap");

从内核对象命名空间的文档来看,它们区分大小写,因此您必须将LOCAL更改为Local才能正常工作。

除了"Global\"前缀之外,客户端进程还可以使用"Local\"前缀在其会话命名空间中显式创建对象。这些关键字区分大小写。

https://msdn.microsoft.com/en-us/library/aa382954(v=vs.85(.aspx

在调试此问题时,我还更改了代码以在映射失败时退出:

if (hFileMap == NULL)
{
    cout << "CreateFileMapping Failed & Error Number - " << GetLastError() << endl;
    return -1;
}
else
    cout << "CreateFileMapping Success - " << endl;

这避免了原始"LOCAL"的崩溃(映射失败(。