1

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.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 << "\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;
}
Prabakar
  • 174
  • 2
  • 4
  • 14
  • 8
    ***Access violation writing location 0x0000000000000000*** Looks like you are dereferencing a null pointer. – drescherjm Jan 17 '18 at 13:22
  • Now it's time for you to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). With a debugger you can catch crashes "in action" and see when and where they happen in your code. And also examine variables and their values. – Some programmer dude Jan 17 '18 at 13:24
  • Press break, search the reason. –  Jan 17 '18 at 13:24
  • 1
    You should exit if the file mapping fails. – drescherjm Jan 17 '18 at 13:25
  • I'm not an expert in visual C++. I'm working on a different platform. I just want to use a shared memory to avoid time overhead.I don't know what to do now. – Prabakar Jan 17 '18 at 13:26
  • BTW, Error code `3` from `GetLastError()` means path not found: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx – drescherjm Jan 17 '18 at 13:26
  • Try actually reading what is written on the console. What exactly do you expect to happen when you see these messages? – n. m. could be an AI Jan 17 '18 at 13:31
  • No. There is no Folder named like that. – Prabakar Jan 17 '18 at 13:31
  • It's just a name. It is not a path. It's the name for the page file for memory mapping. – Prabakar Jan 17 '18 at 13:34
  • But actually it's not a path I think. I just named it like that. see this syntax for create file mapping function : https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537(v=vs.85).aspx – Prabakar Jan 17 '18 at 13:42
  • Sorry you are correct. I have always used this as a path to an existing file for the mapping. I am not sure why local is failing in this case. https://msdn.microsoft.com/en-us/library/windows/desktop/aa366791(v=vs.85).aspx – drescherjm Jan 17 '18 at 13:44
  • Can anyone run the code?. I'm stuck in this. And also I'm not working in visual C++. I just need to use file mapping for shared memory. – Prabakar Jan 17 '18 at 13:49
  • I ran it. The error seems to be caused by capitalization. LOCAL must be Local. After fixing that it worked for me on windows 10. – drescherjm Jan 17 '18 at 14:00
  • Yeah It's working after changing "LOCAL" to "Local".Thank you so much :) – Prabakar Jan 17 '18 at 14:02
  • `if (hFileMap == FALSE)` will work but `if (hFileMap == NULL)` is more accurate. – drescherjm Jan 17 '18 at 14:02
  • I also put `return -1;` after `cout << "CreateFileMapping Failed & Error Number - " << GetLastError() << endl;` to have the program exit if the mapping fails. – drescherjm Jan 17 '18 at 14:03
  • Thank you once again! :) – Prabakar Jan 17 '18 at 14:06

1 Answers1

3

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

drescherjm
  • 10,365
  • 5
  • 44
  • 64