c++visual-studioipcfile-mapping

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


I tried creating two different visual c++ console applications for Inter Process Communication(IPC). The Build for both codes is successful.But,when I try to debug it, I get exception like this "Exception thrown at 0x00007FFF168E1657 (vcruntime140d.dll) in FileMapServer_Parent.exe: 0xC0000005: Access violation writing location 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 << "\t\t.....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;
}

Exception while running parent process: [1]: https://i.sstatic.net/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 << "\t\t.....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;
}

Solution

  • The problem is here:

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

    From the documentation of Kernel object namespaces they are case sensitive so you must change LOCAL to Local for this to work.

    In addition to the "Global\" prefix, client processes can use the "Local\" prefix to explicitly create an object in their session namespace. These keywords are case sensitive.

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

    While debugging this issue I also changed the code to exit if the mapping fails:

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

    This avoided the crash with the original "LOCAL" (mapping failed).