public static unsafe SharedMemory Create(string name, Guid content, List<SecurityIdentifier> allowedSids) { int errorCode = UnsafeNativeMethods.ERROR_SUCCESS; byte[] binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ); SafeFileMappingHandle fileMapping; UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); fixed (byte* pinnedSecurityDescriptor = binarySecurityDescriptor) { securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor; fileMapping = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, UnsafeNativeMethods.PAGE_READWRITE, 0, sizeof(SharedMemoryContents), name); errorCode = Marshal.GetLastWin32Error(); } if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); fileMapping.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(errorCode)); } SharedMemory sharedMemory = new SharedMemory(fileMapping); SafeViewOfFileHandle view; // Ignore return value. GetView(fileMapping, true, out view); try { SharedMemoryContents* contents = (SharedMemoryContents*)view.DangerousGetHandle(); contents->pipeGuid = content; Thread.MemoryBarrier(); contents->isInitialized = true; return sharedMemory; } finally { view.Close(); } }
public static unsafe SharedMemory Create(string name, Guid content, List<SecurityIdentifier> allowedSids) { SafeFileMappingHandle handle; SafeViewOfFileHandle handle2; SharedMemory memory2; int error = 0; byte[] buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, -2147483648); UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); fixed (byte* numRef = buffer) { securityAttributes.lpSecurityDescriptor = (IntPtr) numRef; handle = UnsafeNativeMethods.CreateFileMapping((IntPtr) (-1), securityAttributes, 4, 0, sizeof(SharedMemoryContents), name); error = Marshal.GetLastWin32Error(); } if (handle.IsInvalid) { handle.SetHandleAsInvalid(); handle.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception(error)); } SharedMemory memory = new SharedMemory(handle); GetView(handle, true, out handle2); try { SharedMemoryContents* contentsPtr = (SharedMemoryContents*) handle2.DangerousGetHandle(); contentsPtr->pipeGuid = content; Thread.MemoryBarrier(); contentsPtr->isInitialized = true; memory2 = memory; } finally { handle2.Close(); } return memory2; }
public static unsafe bool TryCreate(List<SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result) { byte[] buffer; SafeFileMappingHandle handle; int num; bool flag2; Guid guid = Guid.NewGuid(); string pipeName = BuildPipeName(guid); try { buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, -2147483648); } catch (Win32Exception exception) { Exception innerException = new PipeException(exception.Message, exception); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } result = null; fixed (byte* numRef = buffer) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES { lpSecurityDescriptor = numRef }; handle = UnsafeNativeMethods.CreateFileMapping((IntPtr) (-1), securityAttributes, 4, 0, sizeof(SharedMemoryContents), sharedMemoryName); num = Marshal.GetLastWin32Error(); } if (handle.IsInvalid) { handle.SetHandleAsInvalid(); if (num == 5) { return false; } Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeNameCantBeReserved", new object[] { pipeUri.AbsoluteUri, PipeError.GetErrorString(num) }), num); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3)); } if (num == 0xb7) { handle.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(num, pipeUri)); } PipeSharedMemory memory = new PipeSharedMemory(handle, pipeUri, pipeName); bool flag = true; try { memory.InitializeContents(guid); flag = false; result = memory; flag2 = true; } finally { if (flag) { memory.Dispose(); } } return flag2; }
private unsafe PipeHandle CreatePipe() { byte[] buffer; PipeHandle handle; int num2; PipeHandle handle2; int openMode = 0x40000003; if (!this.anyPipesCreated) { openMode |= 0x80000; } try { buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(this.allowedSids, -1073741824); } catch (Win32Exception exception) { Exception innerException = new PipeException(exception.Message, exception); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } fixed (byte* numRef = buffer) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES { lpSecurityDescriptor = numRef }; handle = UnsafeNativeMethods.CreateNamedPipe(this.sharedMemory.PipeName, openMode, 6, this.maxInstances, this.bufferSize, this.bufferSize, 0, securityAttributes); num2 = Marshal.GetLastWin32Error(); } if (handle.IsInvalid) { handle.SetHandleAsInvalid(); Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeListenFailed", new object[] { this.pipeUri.AbsoluteUri, PipeError.GetErrorString(num2) }), num2); switch (num2) { case 5: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3)); case 0xb7: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(exception3.Message, exception3)); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(exception3.Message, exception3)); } bool flag = true; try { if (this.useCompletionPort) { ThreadPool.BindHandle(handle); } this.anyPipesCreated = true; flag = false; handle2 = handle; } finally { if (flag) { handle.Close(); } } return handle2; }
private unsafe PipeHandle CreatePipe() { byte[] buffer; PipeHandle handle; int num2; PipeHandle handle2; int openMode = 0x40000003; if (!this.anyPipesCreated) { openMode |= 0x80000; } try { buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(this.allowedSids, -1073741824); } catch (Win32Exception exception) { Exception innerException = new PipeException(exception.Message, exception); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } fixed(byte *numRef = buffer) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES { lpSecurityDescriptor = numRef }; handle = UnsafeNativeMethods.CreateNamedPipe(this.sharedMemory.PipeName, openMode, 6, this.maxInstances, this.bufferSize, this.bufferSize, 0, securityAttributes); num2 = Marshal.GetLastWin32Error(); } if (handle.IsInvalid) { handle.SetHandleAsInvalid(); Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeListenFailed", new object[] { this.pipeUri.AbsoluteUri, PipeError.GetErrorString(num2) }), num2); switch (num2) { case 5: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3)); case 0xb7: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(exception3.Message, exception3)); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(exception3.Message, exception3)); } bool flag = true; try { if (this.useCompletionPort) { ThreadPool.BindHandle(handle); } this.anyPipesCreated = true; flag = false; handle2 = handle; } finally { if (flag) { handle.Close(); } } return(handle2); }
public unsafe static bool TryCreate(List<SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result) { Guid pipeGuid = Guid.NewGuid(); string pipeName = BuildPipeName(pipeGuid.ToString()); byte[] binarySecurityDescriptor; try { binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ); } catch (Win32Exception e) { // While Win32exceptions are not expected, if they do occur we need to obey the pipe/communication exception model. Exception innerException = new PipeException(e.Message, e); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } SafeFileMappingHandle fileMapping; int error; result = null; fixed (byte* pinnedSecurityDescriptor = binarySecurityDescriptor) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor; fileMapping = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, UnsafeNativeMethods.PAGE_READWRITE, 0, sizeof(SharedMemoryContents), sharedMemoryName); error = Marshal.GetLastWin32Error(); } if (fileMapping.IsInvalid) { fileMapping.SetHandleAsInvalid(); if (error == UnsafeNativeMethods.ERROR_ACCESS_DENIED) { return false; } else { Exception innerException = new PipeException(SR.GetString(SR.PipeNameCantBeReserved, pipeUri.AbsoluteUri, PipeError.GetErrorString(error)), error); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(innerException.Message, innerException)); } } // now we have a valid file mapping handle if (error == UnsafeNativeMethods.ERROR_ALREADY_EXISTS) { fileMapping.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(error, pipeUri)); } PipeSharedMemory pipeSharedMemory = new PipeSharedMemory(fileMapping, pipeUri, pipeName); bool disposeSharedMemory = true; try { pipeSharedMemory.InitializeContents(pipeGuid); disposeSharedMemory = false; result = pipeSharedMemory; if (TD.PipeSharedMemoryCreatedIsEnabled()) { TD.PipeSharedMemoryCreated(sharedMemoryName); } return true; } finally { if (disposeSharedMemory) { pipeSharedMemory.Dispose(); } } }
unsafe PipeHandle CreatePipe() { int openMode = UnsafeNativeMethods.PIPE_ACCESS_DUPLEX | UnsafeNativeMethods.FILE_FLAG_OVERLAPPED; if (!anyPipesCreated) { openMode |= UnsafeNativeMethods.FILE_FLAG_FIRST_PIPE_INSTANCE; } byte[] binarySecurityDescriptor; try { binarySecurityDescriptor = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, UnsafeNativeMethods.GENERIC_READ | UnsafeNativeMethods.GENERIC_WRITE); } catch (Win32Exception e) { // While Win32exceptions are not expected, if they do occur we need to obey the pipe/communication exception model. Exception innerException = new PipeException(e.Message, e); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } PipeHandle pipeHandle; int error; string pipeName = null; fixed (byte* pinnedSecurityDescriptor = binarySecurityDescriptor) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES(); securityAttributes.lpSecurityDescriptor = (IntPtr)pinnedSecurityDescriptor; pipeName = this.sharedMemory.PipeName; pipeHandle = UnsafeNativeMethods.CreateNamedPipe( pipeName, openMode, UnsafeNativeMethods.PIPE_TYPE_MESSAGE | UnsafeNativeMethods.PIPE_READMODE_MESSAGE, maxInstances, bufferSize, bufferSize, 0, securityAttributes); error = Marshal.GetLastWin32Error(); } if (pipeHandle.IsInvalid) { pipeHandle.SetHandleAsInvalid(); Exception innerException = new PipeException(SR.GetString(SR.PipeListenFailed, pipeUri.AbsoluteUri, PipeError.GetErrorString(error)), error); if (error == UnsafeNativeMethods.ERROR_ACCESS_DENIED) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(innerException.Message, innerException)); } else if (error == UnsafeNativeMethods.ERROR_ALREADY_EXISTS) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAlreadyInUseException(innerException.Message, innerException)); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } } else { if (TD.NamedPipeCreatedIsEnabled()) { TD.NamedPipeCreated(pipeName); } } bool closePipe = true; try { if (useCompletionPort) { ThreadPool.BindHandle(pipeHandle); } anyPipesCreated = true; closePipe = false; return pipeHandle; } finally { if (closePipe) { pipeHandle.Close(); } } }
public static unsafe bool TryCreate(List <SecurityIdentifier> allowedSids, Uri pipeUri, string sharedMemoryName, out PipeSharedMemory result) { byte[] buffer; SafeFileMappingHandle handle; int num; bool flag2; Guid guid = Guid.NewGuid(); string pipeName = BuildPipeName(guid); try { buffer = SecurityDescriptorHelper.FromSecurityIdentifiers(allowedSids, -2147483648); } catch (Win32Exception exception) { Exception innerException = new PipeException(exception.Message, exception); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(innerException.Message, innerException)); } result = null; fixed(byte *numRef = buffer) { UnsafeNativeMethods.SECURITY_ATTRIBUTES securityAttributes = new UnsafeNativeMethods.SECURITY_ATTRIBUTES { lpSecurityDescriptor = numRef }; handle = UnsafeNativeMethods.CreateFileMapping((IntPtr)(-1), securityAttributes, 4, 0, sizeof(SharedMemoryContents), sharedMemoryName); num = Marshal.GetLastWin32Error(); } if (handle.IsInvalid) { handle.SetHandleAsInvalid(); if (num == 5) { return(false); } Exception exception3 = new PipeException(System.ServiceModel.SR.GetString("PipeNameCantBeReserved", new object[] { pipeUri.AbsoluteUri, PipeError.GetErrorString(num) }), num); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AddressAccessDeniedException(exception3.Message, exception3)); } if (num == 0xb7) { handle.Close(); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreatePipeNameInUseException(num, pipeUri)); } PipeSharedMemory memory = new PipeSharedMemory(handle, pipeUri, pipeName); bool flag = true; try { memory.InitializeContents(guid); flag = false; result = memory; flag2 = true; } finally { if (flag) { memory.Dispose(); } } return(flag2); }