Open
Description
Tried to accomplish this using the example here:
However this generated a DllNotFoundException for kernel32.
Kernel32 is a windows-only thing, looked into source a bit:
public static unsafe RawMemoryMapping<T> OpenRead(string fileName)
{
if (fileName == null)
throw new ArgumentNullException(nameof (fileName));
RawMemoryMapping<T> rawMemoryMapping = new RawMemoryMapping<T>();
rawMemoryMapping.teaFile = TeaFile<T>.OpenRead(fileName);
try
{
if (!(rawMemoryMapping.teaFile.Stream is FileStream stream))
throw new InvalidOperationException("TeaFile used for MemoryMapping is not a file stream but memory mapping requires a file.");
rawMemoryMapping.mappingHandle = UnsafeNativeMethods.CreateFileMapping(stream.SafeFileHandle, IntPtr.Zero, MapProtection.PageReadOnly, 0, 0, (string) null);
if (rawMemoryMapping.mappingHandle.IsInvalid)
throw new Win32Exception();
try
{
rawMemoryMapping.mappingStart = UnsafeNativeMethods.MapViewOfFile(rawMemoryMapping.mappingHandle, MapAccess.FileMapRead, 0, 0, IntPtr.Zero);
if ((IntPtr) rawMemoryMapping.mappingStart == IntPtr.Zero)
throw new Win32Exception();
rawMemoryMapping.itemAreaStart = rawMemoryMapping.mappingStart + rawMemoryMapping.teaFile.ItemAreaStart;
rawMemoryMapping.itemAreaEnd = rawMemoryMapping.itemAreaStart + rawMemoryMapping.teaFile.ItemAreaSize;
}
catch
{
rawMemoryMapping.mappingHandle = UnsafeNativeMethods.CloseHandle(rawMemoryMapping.mappingHandle) != 0 ? (SafeFileHandle) null : throw new Win32Exception();
throw;
}
return rawMemoryMapping;
}
catch
{
rawMemoryMapping.Dispose();
throw;
}
}
internal static class UnsafeNativeMethods
{
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern SafeFileHandle CreateFileMapping(
SafeFileHandle hFile,
IntPtr lpAttributes,
MapProtection flProtect,
int dwMaximumSizeHigh,
int dwMaximumSizeLow,
string lpName);
//...
}
So it's the calls inside this UnsafeNativeMethods which won't work as implemented.
In recent dotnets, is it possible to reimplement UnsafeNativeMethods using MemoryMappedFile class?
Not all of MemoryMappedFile classes methods will run outside of windows, but MemoryMappedFile.CreateFromFile does work.
Any ideas or suggestions on how this might be accomplished?
Metadata
Metadata
Assignees
Labels
No labels