Q: Shared Memory with Memory Mapped File I am creating two apps: one in C# and one in C++, and I would like to use shared memory for IPC between two apps. For shared memory, I used Memory-Mapped File.
The C# app will create MemoryMappedFile and write data to it:
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("testmap", 256))
            {
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    BinaryWriter writer = new BinaryWriter(stream);
                    writer.Write("Hello, world!");
                }
            }

            Console.ReadKey();
        }
    }
}

The C++ app will then read the value from the shared memory:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")

#define BUF_SIZE 256
TCHAR szName[] = TEXT("testmap");

int _tmain()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

    hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,   // read/write access
        FALSE,                 // do not inherit the name
        szName);               // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not open file mapping object (%d).\n"),
            GetLastError());
        return 1;
    }

    pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
        FILE_MAP_ALL_ACCESS,  // read/write permission
        0,
        0,
        BUF_SIZE);

    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
            GetLastError());

        CloseHandle(hMapFile);

        return 1;
    }

    MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

    UnmapViewOfFile(pBuf);

    CloseHandle(hMapFile);

    return 0;
}

First, I will run C# app to create shared memory, then run C++ app to read data from shared memory. But I get exception in C++ app: "Could not open file mapping object (2)". Is it because Memory-Mapped File in .net is not compatible with C++ Memory-Mapped File I am using here? If so, is there any library that I can use to shared memory between C++ and C# apps? Or I made mistakes somewhere in the code?
Upadted: 
It was a bug in my C# code, the Console.ReadKey() should be inside the using block! Thanks to Severin.
