gpt4 book ai didi

c# - 使用内存映射文件 (IOException) 的进程间通信(C++ 到 C#)

转载 作者:行者123 更新时间:2023-12-05 04:34:26 36 4
gpt4 key购买 nike

有两个进程:一个是用C++写的,另一个是用C#写的。

简单来说,C++进程会创建一个名为“test.dat”的文件,将文件映射到内存中,并继续写入。

另一方面,只要内存发生变化,C# 进程就会打开文件并读取。

问题是在 C# 端,它不允许我打开文件。 (IOException 表示另一个进程正在使用中”

这是我测试过的。

//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# 部分:

        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);
}
}
}

关于处理两个进程之间的文件共享有什么想法吗?

最佳答案

文件流 defaults to只允许与您打开 C++ 文件的方式不兼容的读取共享。您也需要请求写入共享:

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

关于c# - 使用内存映射文件 (IOException) 的进程间通信(C++ 到 C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71232149/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com