[System.Security.SecurityCritical] // auto-generated_required public unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity) { if (null != name && System.IO.Path.MAX_PATH < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name)); } Contract.EndContractBlock(); Win32Native.SECURITY_ATTRIBUTES secAttrs = null; #if FEATURE_MACL // For ACL's, get the security descriptor from the EventWaitHandleSecurity. if (eventSecurity != null) { secAttrs = new Win32Native.SECURITY_ATTRIBUTES(); secAttrs.nLength = (int)Marshal.SizeOf(secAttrs); byte[] sd = eventSecurity.GetSecurityDescriptorBinaryForm(); byte * pSecDescriptor = stackalloc byte[sd.Length]; Buffer.Memcpy(pSecDescriptor, 0, sd, 0, sd.Length); secAttrs.pSecurityDescriptor = pSecDescriptor; } #endif SafeWaitHandle _handle = null; Boolean isManualReset; switch (mode) { case EventResetMode.ManualReset: isManualReset = true; break; case EventResetMode.AutoReset: isManualReset = false; break; default: throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", name)); } ; _handle = Win32Native.CreateEvent(secAttrs, isManualReset, initialState, name); int errorCode = Marshal.GetLastWin32Error(); if (_handle.IsInvalid) { _handle.SetHandleAsInvalid(); if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode) { throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name)); } __Error.WinIOError(errorCode, name); } createdNew = errorCode != Win32Native.ERROR_ALREADY_EXISTS; SetHandleInternal(_handle); }
public unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity) { if (name != null && 260 < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name })); } Win32Native.SECURITY_ATTRIBUTES security_ATTRIBUTES = null; if (eventSecurity != null) { security_ATTRIBUTES = new Win32Native.SECURITY_ATTRIBUTES(); security_ATTRIBUTES.nLength = Marshal.SizeOf <Win32Native.SECURITY_ATTRIBUTES>(security_ATTRIBUTES); byte[] securityDescriptorBinaryForm = eventSecurity.GetSecurityDescriptorBinaryForm(); byte * ptr = stackalloc byte[checked (unchecked ((UIntPtr)securityDescriptorBinaryForm.Length) * 1)]; Buffer.Memcpy(ptr, 0, securityDescriptorBinaryForm, 0, securityDescriptorBinaryForm.Length); security_ATTRIBUTES.pSecurityDescriptor = ptr; } bool isManualReset; if (mode != EventResetMode.AutoReset) { if (mode != EventResetMode.ManualReset) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", new object[] { name })); } isManualReset = true; } else { isManualReset = false; } SafeWaitHandle safeWaitHandle = Win32Native.CreateEvent(security_ATTRIBUTES, isManualReset, initialState, name); int lastWin32Error = Marshal.GetLastWin32Error(); if (safeWaitHandle.IsInvalid) { safeWaitHandle.SetHandleAsInvalid(); if (name != null && name.Length != 0 && 6 == lastWin32Error) { throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name })); } __Error.WinIOError(lastWin32Error, name); } createdNew = (lastWin32Error != 183); base.SetHandleInternal(safeWaitHandle); }
public unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity) { bool flag; if ((name != null) && (260 < name.Length)) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name })); } Win32Native.SECURITY_ATTRIBUTES structure = null; if (eventSecurity != null) { structure = new Win32Native.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; byte[] securityDescriptorBinaryForm = eventSecurity.GetSecurityDescriptorBinaryForm(); byte * pDest = stackalloc byte[(IntPtr)securityDescriptorBinaryForm.Length]; Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length); structure.pSecurityDescriptor = pDest; } SafeWaitHandle handle = null; switch (mode) { case EventResetMode.AutoReset: flag = false; break; case EventResetMode.ManualReset: flag = true; break; default: throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", new object[] { name })); } handle = Win32Native.CreateEvent(structure, flag, initialState, name); int errorCode = Marshal.GetLastWin32Error(); if (handle.IsInvalid) { handle.SetHandleAsInvalid(); if (((name != null) && (name.Length != 0)) && (6 == errorCode)) { throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name })); } __Error.WinIOError(errorCode, name); } createdNew = errorCode != 0xb7; base.SetHandleInternal(handle); }
public unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity) { if (name != null && 260 < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", (object)name)); } Win32Native.SECURITY_ATTRIBUTES securityAttributes = (Win32Native.SECURITY_ATTRIBUTES)null; if (eventSecurity != null) { securityAttributes = new Win32Native.SECURITY_ATTRIBUTES(); securityAttributes.nLength = Marshal.SizeOf <Win32Native.SECURITY_ATTRIBUTES>(securityAttributes); byte[] descriptorBinaryForm = eventSecurity.GetSecurityDescriptorBinaryForm(); byte * pDest = stackalloc byte[descriptorBinaryForm.Length]; Buffer.Memcpy(pDest, 0, descriptorBinaryForm, 0, descriptorBinaryForm.Length); securityAttributes.pSecurityDescriptor = pDest; } bool isManualReset; if (mode != EventResetMode.AutoReset) { if (mode == EventResetMode.ManualReset) { isManualReset = true; } else { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", (object)name)); } } else { isManualReset = false; } SafeWaitHandle @event = Win32Native.CreateEvent(securityAttributes, isManualReset, initialState, name); int lastWin32Error = Marshal.GetLastWin32Error(); if (@event.IsInvalid) { @event.SetHandleAsInvalid(); if (name != null && name.Length != 0 && 6 == lastWin32Error) { throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", (object)name)); } __Error.WinIOError(lastWin32Error, name); } createdNew = lastWin32Error != 183; this.SetHandleInternal(@event); }
/// <summary>Gets or creates an <see cref="EventWaitHandle" /> instance, allowing a <see cref="EventWaitHandleSecurity " /> instance to be optionally specified to set it during the event creation.</summary> /// <param name="initialState"><see langword="true" /> to set the initial state to signaled if the named event is created as a result of this call; <see langword="false" /> to set it to non-signaled.</param> /// <param name="mode">One of the enum values that determines whether the event resets automatically or manually.</param> /// <param name="name">The name, if the event is a system-wide synchronization event; otherwise, <see langword="null" /> or an empty string.</param> /// <param name="createdNew">When this method returns, this argument is always set to <see langword="true" /> if a local event is created; that is, when <paramref name="name" /> is <see langword="null" /> or <see cref="string.Empty" />. If <paramref name="name" /> has a valid, non-empty value, this argument is set to <see langword="true" /> when the system event is created, or it is set to <see langword="false" /> if an existing system event is found with that name. This parameter is passed uninitialized.</param> /// <param name="eventSecurity">The optional Windows access control security to apply.</param> /// <returns>An object that represents a system event wait handle, if named, or a local event wait handle, if nameless.</returns> /// <exception cref="ArgumentNullException">.NET Framework only: The <paramref name="name" /> length is beyond MAX_PATH (260 characters).</exception> /// <exception cref="ArgumentOutOfRangeException">The <paramref name="mode" /> enum value was out of legal range.</exception> /// <exception cref="DirectoryNotFoundException">Could not find a part of the path specified in <paramref name="name" />.</exception> /// <exception cref="WaitHandleCannotBeOpenedException">A system-wide synchronization event with the provided <paramref name="name" /> was not found. /// -or- /// An <see cref="EventWaitHandle" /> with system-wide name <paramref name="name" /> cannot be created. An <see cref="EventWaitHandle" /> of a different type might have the same name.</exception> /// <remarks>If a `name` is passed and the system event already exists, the existing event is returned. If `name` is `null` or <see cref="string.Empty" />, a new local event is always created.</remarks> public static unsafe EventWaitHandle Create(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity) { if (eventSecurity == null) { return(new EventWaitHandle(initialState, mode, name, out createdNew)); } if (mode != EventResetMode.AutoReset && mode != EventResetMode.ManualReset) { throw new ArgumentOutOfRangeException(nameof(mode)); } uint eventFlags = initialState ? Interop.Kernel32.CREATE_EVENT_INITIAL_SET : 0; if (mode == EventResetMode.ManualReset) { eventFlags |= Interop.Kernel32.CREATE_EVENT_MANUAL_RESET; } fixed(byte *pSecurityDescriptor = eventSecurity.GetSecurityDescriptorBinaryForm()) { var secAttrs = new Interop.Kernel32.SECURITY_ATTRIBUTES { nLength = (uint)sizeof(Interop.Kernel32.SECURITY_ATTRIBUTES), lpSecurityDescriptor = (IntPtr)pSecurityDescriptor }; SafeWaitHandle handle = Interop.Kernel32.CreateEventEx( (IntPtr)(&secAttrs), name, eventFlags, (uint)EventWaitHandleRights.FullControl); ValidateHandle(handle, name, out createdNew); EventWaitHandle ewh = new EventWaitHandle(initialState, mode); SafeWaitHandle old = ewh.SafeWaitHandle; ewh.SafeWaitHandle = handle; old.Dispose(); return(ewh); } }
private SafeFileHandle CreateFileMapping(string fileMappingName) { unsafe { IntPtr securityAttributesPtr = IntPtr.Zero; #if !NETSTANDARD // We use the EventWaitHandleSecurity only to generate the descriptor binary form // that does not differ for object types, so we abuse the existing "event handle" implementation, // not to have to create the file mapping SecurityAttributes via P/Invoke. // .NET 4 supports MemoryMappedFile and MemoryMappedFileSecurity natively already EventWaitHandleSecurity security = CreateSecurity((EventWaitHandleRights)FileMappingRights.AllAccess); if (security != null) { SecurityAttributes securityAttributes = new SecurityAttributes(); securityAttributes.nLength = (uint)Marshal.SizeOf(securityAttributes); byte[] descriptorBinaryForm = security.GetSecurityDescriptorBinaryForm(); byte * buffer = stackalloc byte[descriptorBinaryForm.Length]; for (int i = 0; i < descriptorBinaryForm.Length; i++) { buffer[i] = descriptorBinaryForm[i]; } securityAttributes.lpSecurityDescriptor = (IntPtr)buffer; int length = Marshal.SizeOf(typeof(SecurityAttributes)); securityAttributesPtr = Marshal.AllocHGlobal(length); Marshal.StructureToPtr(securityAttributes, securityAttributesPtr, false); } #endif return (UnsafeNativeMethods.CreateFileMapping( new SafeFileHandle(new IntPtr(-1), true), securityAttributesPtr, FileMapProtection.PageReadWrite, 0, ConsoleCommStruct.Size, fileMappingName)); } }