/// <summary>
/// Used by the OpenExisting factory method group and by CreateOrOpen if access is write.
/// We'll throw an ArgumentException if the file mapping object didn't exist and the
/// caller used CreateOrOpen since Create isn't valid with Write access
/// </summary>
private static SafeMemoryMappedFileHandle OpenCore(
string mapName, HandleInheritability inheritability, int desiredAccessRights, bool createOrOpen)
{
SafeMemoryMappedFileHandle handle = Interop.OpenFileMapping(
desiredAccessRights, (inheritability & HandleInheritability.Inheritable) != 0, mapName);
int lastError = Marshal.GetLastWin32Error();
if (handle.IsInvalid)
{
handle.Dispose();
if (createOrOpen && (lastError == Interop.Errors.ERROR_FILE_NOT_FOUND))
{
throw new ArgumentException(SR.Argument_NewMMFWriteAccessNotAllowed, "access");
}
else
{
throw Win32Marshal.GetExceptionForWin32Error(lastError);
}
}
return(handle);
}