Hay dos procesos: uno escrito en C++ y el otro escrito en C#.
Simplemente, el proceso de C++ creará un nombre de archivo "test.dat", asignará el archivo a su memoria y seguirá escribiendo en él.
El proceso de C#, por otro lado, abrirá el archivo y lo leerá cada vez que haya un cambio en la memoria.
El problema es que en el extremo C# no me deja abrir el archivo. (IOException que dice que el otro proceso está en uso"
Esto es lo que he probado.
//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)); } } }Parte de C# a continuación:
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); } } }¿Alguna idea sobre cómo abordar el intercambio de archivos entre los dos procesos?