简体   繁体   English

使用 Memory 映射文件 (IOException) 的进程间通信(C++ 到 C#)

[英]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#.有两个进程:一个写在C++,一个写在C#。

Simply, C++ process will create a file name "test.dat", map the file to its memory, and keep on writing on it.简单的说,C++进程会创建一个文件名为“test.dat”,map这个文件给它的memory,然后一直写就可以了。

C# process on the other hand will open the file and read whenever there is a change on the memory.另一方面,只要 memory 发生变化,C# 进程就会打开文件并读取。

The problem is that on the C# end, it does not let me open the file.问题是在 C# 端,它不让我打开文件。 (IOException saying the other process is in use" (IOException 表示另一个进程正在使用中”

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: C# 以下部分:

        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?关于处理两个进程之间的文件共享的任何想法?

FileStream defaults to only allowing read sharing which is not compatible with how you've opened your c++ file. FileStream 默认仅允许读取共享,这与您打开 c++ 文件的方式不兼容。 You need to request write sharing too:您也需要请求写入共享:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM