private static SafeMemoryMappedFileHandle CreateCore(
FileStream fileStream, string mapName, HandleInheritability inheritability,
MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity)
{
SafeFileHandle fileHandle = fileStream != null ? fileStream.SafeFileHandle : null;
Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = GetSecAttrs(inheritability);
SafeMemoryMappedFileHandle handle = fileHandle != null ?
Interop.CreateFileMapping(fileHandle, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName) :
Interop.CreateFileMapping(INVALID_HANDLE_VALUE, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName);
int errorCode = Marshal.GetLastWin32Error();
if (!handle.IsInvalid)
{
if (errorCode == Interop.Errors.ERROR_ALREADY_EXISTS)
{
handle.Dispose();
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
}
else // handle.IsInvalid
{
handle.Dispose();
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
}
return handle;
}