public unsafe Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity) { SafeWaitHandle handle; if (initialCount < 0) { throw new ArgumentOutOfRangeException("initialCount", SR.GetString("ArgumentOutOfRange_NeedNonNegNumRequired")); } if (maximumCount < 1) { throw new ArgumentOutOfRangeException("maximumCount", SR.GetString("ArgumentOutOfRange_NeedNonNegNumRequired")); } if (initialCount > maximumCount) { throw new ArgumentException(SR.GetString("Argument_SemaphoreInitialMaximum")); } if ((name != null) && (MAX_PATH < name.Length)) { throw new ArgumentException(SR.GetString("Argument_WaitHandleNameTooLong")); } if (semaphoreSecurity != null) { Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES structure = null; structure = new Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; fixed(byte *numRef = semaphoreSecurity.GetSecurityDescriptorBinaryForm()) { structure.lpSecurityDescriptor = new SafeLocalMemHandle((IntPtr)numRef, false); handle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(structure, initialCount, maximumCount, name); } } else { handle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(null, initialCount, maximumCount, name); } int num = Marshal.GetLastWin32Error(); if (handle.IsInvalid) { if (((name != null) && (name.Length != 0)) && (6 == num)) { throw new WaitHandleCannotBeOpenedException(SR.GetString("WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name })); } InternalResources.WinIOError(); } createdNew = num != 0xb7; base.SafeWaitHandle = handle; }
public unsafe Semaphore(int initialCount, int maximumCount, string name, out bool createdNew, SemaphoreSecurity semaphoreSecurity) { SafeWaitHandle handle; if (initialCount < 0) { throw new ArgumentOutOfRangeException("initialCount", SR.GetString("ArgumentOutOfRange_NeedNonNegNumRequired")); } if (maximumCount < 1) { throw new ArgumentOutOfRangeException("maximumCount", SR.GetString("ArgumentOutOfRange_NeedNonNegNumRequired")); } if (initialCount > maximumCount) { throw new ArgumentException(SR.GetString("Argument_SemaphoreInitialMaximum")); } if ((name != null) && (MAX_PATH < name.Length)) { throw new ArgumentException(SR.GetString("Argument_WaitHandleNameTooLong")); } if (semaphoreSecurity != null) { Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES structure = null; structure = new Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES { nLength = Marshal.SizeOf(structure) }; fixed (byte* numRef = semaphoreSecurity.GetSecurityDescriptorBinaryForm()) { structure.lpSecurityDescriptor = new SafeLocalMemHandle((IntPtr) numRef, false); handle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(structure, initialCount, maximumCount, name); } } else { handle = Microsoft.Win32.SafeNativeMethods.CreateSemaphore(null, initialCount, maximumCount, name); } int num = Marshal.GetLastWin32Error(); if (handle.IsInvalid) { if (((name != null) && (name.Length != 0)) && (6 == num)) { throw new WaitHandleCannotBeOpenedException(SR.GetString("WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name })); } InternalResources.WinIOError(); } createdNew = num != 0xb7; base.SafeWaitHandle = handle; }
internal static extern bool DuplicateTokenEx(SafeHandle hToken, int access, Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES tokenAttributes, int impersonationLevel, int tokenType, out SafeUserTokenHandle hNewToken);
private void Initialize(string fileMappingName, int fileMappingSize, int initialOffset) { string lpName = fileMappingName; SharedUtils.CheckEnvironment(); SafeLocalMemHandle pSecurityDescriptor = null; new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); try { string stringSecurityDescriptor = "D:(A;OICI;FRFWGRGW;;;AU)(A;OICI;FRFWGRGW;;;S-1-5-33)"; if (!SafeLocalMemHandle.ConvertStringSecurityDescriptorToSecurityDescriptor(stringSecurityDescriptor, 1, out pSecurityDescriptor, IntPtr.Zero)) { throw new InvalidOperationException(SR.GetString("SetSecurityDescriptorFailed")); } Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES lpFileMappingAttributes = new Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES { lpSecurityDescriptor = pSecurityDescriptor, bInheritHandle = false }; int num = 14; int millisecondsTimeout = 0; bool flag = false; while (!flag && (num > 0)) { this.fileMappingHandle = Microsoft.Win32.NativeMethods.CreateFileMapping((IntPtr) (-1), lpFileMappingAttributes, 4, 0, fileMappingSize, lpName); if ((Marshal.GetLastWin32Error() != 5) || !this.fileMappingHandle.IsInvalid) { flag = true; } else { this.fileMappingHandle.SetHandleAsInvalid(); this.fileMappingHandle = Microsoft.Win32.NativeMethods.OpenFileMapping(2, false, lpName); if ((Marshal.GetLastWin32Error() != 2) || !this.fileMappingHandle.IsInvalid) { flag = true; continue; } num--; if (millisecondsTimeout == 0) { millisecondsTimeout = 10; } else { Thread.Sleep(millisecondsTimeout); millisecondsTimeout *= 2; } } } if (this.fileMappingHandle.IsInvalid) { throw new InvalidOperationException(SR.GetString("CantCreateFileMapping")); } this.fileViewAddress = SafeFileMapViewHandle.MapViewOfFile(this.fileMappingHandle, 2, 0, 0, UIntPtr.Zero); if (this.fileViewAddress.IsInvalid) { throw new InvalidOperationException(SR.GetString("CantMapFileView")); } Microsoft.Win32.NativeMethods.MEMORY_BASIC_INFORMATION buffer = new Microsoft.Win32.NativeMethods.MEMORY_BASIC_INFORMATION(); if (Microsoft.Win32.NativeMethods.VirtualQuery(this.fileViewAddress, ref buffer, (IntPtr) sizeof(Microsoft.Win32.NativeMethods.MEMORY_BASIC_INFORMATION)) == IntPtr.Zero) { throw new InvalidOperationException(SR.GetString("CantGetMappingSize")); } this.FileMappingSize = (int) ((uint) buffer.RegionSize); } finally { if (pSecurityDescriptor != null) { pSecurityDescriptor.Close(); } CodeAccessPermission.RevertAssert(); } Microsoft.Win32.SafeNativeMethods.InterlockedCompareExchange(this.fileViewAddress.DangerousGetHandle(), initialOffset, 0); }
internal static extern SafeWaitHandle CreateSemaphore(Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES lpSecurityAttributes, int initialCount, int maximumCount, string name);
private void CreatePipe(out SafeFileHandle parentHandle, out SafeFileHandle childHandle, bool parentInputs) { Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES lpPipeAttributes = new Microsoft.Win32.NativeMethods.SECURITY_ATTRIBUTES { bInheritHandle = true }; SafeFileHandle hWritePipe = null; try { if (parentInputs) { CreatePipeWithSecurityAttributes(out childHandle, out hWritePipe, lpPipeAttributes, 0); } else { CreatePipeWithSecurityAttributes(out hWritePipe, out childHandle, lpPipeAttributes, 0); } if (!Microsoft.Win32.NativeMethods.DuplicateHandle(new HandleRef(this, Microsoft.Win32.NativeMethods.GetCurrentProcess()), hWritePipe, new HandleRef(this, Microsoft.Win32.NativeMethods.GetCurrentProcess()), out parentHandle, 0, false, 2)) { throw new Win32Exception(); } } finally { if ((hWritePipe != null) && !hWritePipe.IsInvalid) { hWritePipe.Close(); } } }