public EventWaitHandle(bool initialState, EventResetMode mode, string name) { if (name != null && 260 < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", (object)name)); } SafeWaitHandle @event; if (mode != EventResetMode.AutoReset) { if (mode == EventResetMode.ManualReset) { @event = Win32Native.CreateEvent((Win32Native.SECURITY_ATTRIBUTES)null, true, initialState, name); } else { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", (object)name)); } } else { @event = Win32Native.CreateEvent((Win32Native.SECURITY_ATTRIBUTES)null, false, initialState, name); } if (@event.IsInvalid) { int lastWin32Error = Marshal.GetLastWin32Error(); @event.SetHandleAsInvalid(); if (name != null && name.Length != 0 && 6 == lastWin32Error) { throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", (object)name)); } __Error.WinIOError(lastWin32Error, name); } this.SetHandleInternal(@event); }
public EventWaitHandle(bool initialState, EventResetMode mode, string name) { if ((name != null) && (260 < name.Length)) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name })); } SafeWaitHandle handle = null; switch (mode) { case EventResetMode.AutoReset: handle = Win32Native.CreateEvent(null, false, initialState, name); break; case EventResetMode.ManualReset: handle = Win32Native.CreateEvent(null, true, initialState, name); break; default: throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", new object[] { name })); } if (handle.IsInvalid) { int errorCode = Marshal.GetLastWin32Error(); 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); } base.SetHandleInternal(handle); }
[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 EventWaitHandle(bool initialState, EventResetMode mode, string name) { if (null != name && System.IO.Path.MAX_PATH < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name)); } Contract.EndContractBlock(); SafeWaitHandle _handle = null; #if MONO int errorCode; #endif switch (mode) { case EventResetMode.ManualReset: #if MONO _handle = new SafeWaitHandle(NativeEventCalls.CreateEvent_internal(true, initialState, name, out errorCode), true); #else _handle = Win32Native.CreateEvent(null, true, initialState, name); #endif break; case EventResetMode.AutoReset: #if MONO _handle = new SafeWaitHandle(NativeEventCalls.CreateEvent_internal(false, initialState, name, out errorCode), true); #else _handle = Win32Native.CreateEvent(null, false, initialState, name); #endif break; default: throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", name)); } ; if (_handle.IsInvalid) { #if !MONO int errorCode = Marshal.GetLastWin32Error(); #endif _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); } SetHandleInternal(_handle); }
[System.Security.SecurityCritical] // auto-generated_required public unsafe EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity) { if (name != null) { #if PLATFORM_UNIX throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_NamedSynchronizationPrimitives")); #else if (System.IO.Path.MaxPath < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", Path.MaxPath), nameof(name)); } #endif } Contract.EndContractBlock(); Win32Native.SECURITY_ATTRIBUTES secAttrs = null; 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) { 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); }
[System.Security.SecurityCritical] // auto-generated_required public EventWaitHandle(bool initialState, EventResetMode mode, string name) { if (name != null) { #if PLATFORM_UNIX throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_NamedSynchronizationPrimitives")); #else if (System.IO.Path.MaxPath < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name)); } #endif } Contract.EndContractBlock(); SafeWaitHandle _handle = null; switch (mode) { case EventResetMode.ManualReset: _handle = Win32Native.CreateEvent(null, true, initialState, name); break; case EventResetMode.AutoReset: _handle = Win32Native.CreateEvent(null, false, initialState, name); break; default: throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", name)); } ; if (_handle.IsInvalid) { int errorCode = Marshal.GetLastWin32Error(); _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); } SetHandleInternal(_handle); }
public EventWaitHandle(bool initialState, EventResetMode mode, string name, out bool createdNew) { if (null != name && System.IO.Path.MAX_PATH < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name)); } Win32Native.SECURITY_ATTRIBUTES secAttrs = null; 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 EventWaitHandle(bool initialState, EventResetMode mode, string name) { if (name != null && 260 < name.Length) { throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name })); } SafeWaitHandle safeWaitHandle; if (mode != EventResetMode.AutoReset) { if (mode != EventResetMode.ManualReset) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag", new object[] { name })); } safeWaitHandle = Win32Native.CreateEvent(null, true, initialState, name); } else { safeWaitHandle = Win32Native.CreateEvent(null, false, initialState, name); } if (safeWaitHandle.IsInvalid) { int lastWin32Error = Marshal.GetLastWin32Error(); 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); } base.SetHandleInternal(safeWaitHandle); }
public override void Initialize(DebugStreamContext context) { _scope = context.GlobalScope ? @"Global\" : @"Local\"; _logger.Information("Initializing debug listener..."); // Initialize the security descriptor. _logger.Verbose("Creating new security descriptor..."); var sd = new SecurityDescriptor(); if (!Win32Native.InitializeSecurityDescriptor(ref sd, Win32Native.SECURITY_DESCRIPTOR_REVISION)) { _logger.Error("Failed to initializes the security descriptor."); throw new InvalidOperationException("Failed to initializes the security descriptor."); } // Set information in a discretionary access control list _logger.Verbose("Creating new security descriptor dacl..."); if (!Win32Native.SetSecurityDescriptorDacl(ref sd, true, IntPtr.Zero, false)) { _logger.Error("Failed to initializes the security descriptor."); throw new InvalidOperationException("Failed to initializes the security descriptor."); } _logger.Verbose("Creating security attributes..."); var sa = new SecurityAttributes(); sa.nLength = Marshal.SizeOf(sa); sa.lpSecurityDescriptor = Marshal.AllocHGlobal(Marshal.SizeOf(sd)); Marshal.StructureToPtr(sd, sa.lpSecurityDescriptor, false); // Create the event for slot 'DBWIN_BUFFER_READY' _logger.Verbose("Creating event DBWIN_BUFFER_READY..."); _bufferReadyEvent = Win32Native.CreateEvent(ref sa, false, false, _scope + "DBWIN_BUFFER_READY"); if (_bufferReadyEvent == IntPtr.Zero) { _logger.Error("Failed to create event DBWIN_BUFFER_READY."); throw new InvalidOperationException("Failed to create event 'DBWIN_BUFFER_READY'"); } // Create the event for slot 'DBWIN_DATA_READY' _logger.Verbose("Creating event DBWIN_DATA_READY..."); _readyEvent = Win32Native.CreateEvent(ref sa, false, false, _scope + "DBWIN_DATA_READY"); if (_readyEvent == IntPtr.Zero) { _logger.Error("Failed to create event DBWIN_DATA_READY."); throw new InvalidOperationException("Failed to create event 'DBWIN_DATA_READY'"); } // Get a handle to the readable shared memory at slot 'DBWIN_BUFFER'. _logger.Verbose("Creating file mapping to slot DBWIN_BUFFER..."); _sharedFile = Win32Native.CreateFileMapping(new IntPtr(-1), ref sa, PageProtection.ReadWrite, 0, 4096, _scope + "DBWIN_BUFFER"); if (_sharedFile == IntPtr.Zero) { _logger.Error("Failed to create a file mapping to slot DBWIN_BUFFER."); throw new InvalidOperationException("Failed to create a file mapping to slot 'DBWIN_BUFFER'"); } // Create a view for this file mapping so we can access it _logger.Verbose("Creating mapping view to slot DBWIN_BUFFER..."); _sharedMemory = Win32Native.MapViewOfFile(_sharedFile, Win32Native.SECTION_MAP_READ, 0, 0, 512); if (_sharedMemory == IntPtr.Zero) { _logger.Error("Failed to create a mapping view to slot DBWIN_BUFFER."); throw new InvalidOperationException("Failed to create a mapping view for slot 'DBWIN_BUFFER'"); } _logger.Information("Debug listener has been initialized."); }