Search code examples
c#c++memory-mapped-files

Inter-process communication (C++ to C#) using Memory mapped file (IOException)


There are two processes: one written in C++ and the other written in C#.

Simply, C++ process will create a file name "test.dat", map the file to its memory, and keep on writing on it.

C# process on the other hand will open the file and read whenever there is a change on the memory.

The problem is that on the C# end, it does not let me open the file. (IOException saying the other process is in use"

Here is what I've tested.

//C++
// Test create & open
void CTestDlg::OnBnClickedBtn1()
{
    WCHAR wcsDebug[1024];
    HANDLE hFile, hMapFile;

    UINT size = sizeof(syncData);

    hFile = CreateFile(L"C:\\Users\\test\\Documents\\2134\\test.dat", GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile != INVALID_HANDLE_VALUE)
    {
        hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, size, NULL);

        if (hMapFile)
        {
            g_pb = (BYTE*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
        }
    }
}

// Test Writing 
void CLibTestDlg::OnBnClickedBtn2()
{
    WCHAR sz[] = L"C:\\Users\\test\\Documents\\2134\\";
    WCHAR wcsFilePath[MAX_PATH];

    CString str;
    GetDlgItemText(IDC_EDIT_FID, str);


    if (str != L"\0")
    {
        swprintf_s(wcsFilePath, _countof(wcsFilePath), L"%s%s", sz, str.GetBuffer());

        if (g_pb)
        {
            syncData sd;

            sd.dwCurrent = nCurr;
            sd.dwTotal = 15;
            sd.eSyncType = TYPE_DOWNLOAD;

            StringCchCopy(sd.wcsFileName, _countof(sd.wcsFileName), wcsFilePath);
            sd.eReqType = TYPE_DOWNLOAD;
            sd.ullTimeStamp = GetTickCount();

            nCurr++;

            memcpy(g_pb, &sd, sizeof(sd));
        }
    }
}

C# portion below:

        private void Button_MouseUp(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Click");

            FileStream fs = File.Open(@"C:\\Users\\test\\Documents\\2134\\test.dat", FileMode.Open, FileAccess.ReadWrite); // Error Here! IOException: The file is being used by another process.

            using (var mmf = MemoryMappedFile.CreateFromFile(fs, "mmf", 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true))
            {
                using (var accessor = mmf.CreateViewAccessor(0, 544))
                {
                    DWORD dwCurrent;
                    accessor.Read(0, out dwCurrent);

                    Console.WriteLine("Hello" + dwCurrent);
                }
            }
        }

Any ideas on approaching the file sharing between the two processes?


Solution

  • FileStream defaults to only allowing read sharing which is not compatible with how you've opened your c++ file. You need to request write sharing too:

    File.Open(@"...", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);