I plan to open a file using memory mapping.
The file is already open by another process in the same way i.e. it has its own memory map view open and from time to time edits the file.
I wish to edit the same file myself and share access to it with the other process as effectively as possible without hopefully conflicting by each process over-writing changes made by the other.
I can first of all open the file directly:
IntPtr f_ptr = CreateFile(
path,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_ALWAYS,
FILE_FLAG_RANDOM_ACCESS,
IntPtr.Zero);
Editing the file binary directly, however, is ineffective in that the other process's memory mapping objects are liable to over-write my changes.
If I open my own file mapping and my own view, as below, the other process seems to become automatically updated with my edits in such a way that it does not overwrite my edits.
What synchronicity is going on here?
It is not that I have opened my own mapped view to the other processes' file mapping. I have created an entirely new FileMapping on the same file.
The filesystem or FileMapping system seems to somehow understand this. Why?
// file map pointer
IntPtr m_ptr = CreateFileMapping(f_ptr, IntPtr.Zero, PAGE_READWRITE, 0, 0, "MyMapping");
// map view pointer
IntPtr view_ptr = MapViewOfFileEx(m_ptr, FILE_MAP_WRITE, 0, 0, 0, IntPtr.Zero);
// EDIT FILE CONTENTS
FlushViewOfFile(view_ptr, 0);
UnmapViewOfFile(view_ptr);
CloseHandle(m_ptr);