private void CreatePipe(out SafeFileHandle parentHandle, out SafeFileHandle childHandle, bool parentInputs) { var securityAttributesParent = new Native.SECURITY_ATTRIBUTES(); securityAttributesParent.bInheritHandle = true; SafeFileHandle hTmp = null; try { CreatePipeWithSecurityAttributes(out hTmp, out childHandle, securityAttributesParent, 0); // Duplicate the parent handle to be non-inheritable so that the child process // doesn't have access. This is done for correctness sake, exact reason is unclear. // One potential theory is that child process can do something brain dead like // closing the parent end of the pipe and there by getting into a blocking situation // as parent will not be draining the pipe at the other end anymore. if (!Native.DuplicateHandle(new HandleRef(this, Native.GetCurrentProcess()), hTmp, new HandleRef(this, Native.GetCurrentProcess()), out parentHandle, 0, false, Native.DUPLICATE_SAME_ACCESS)) { throw new Win32Exception(); } } finally { if (hTmp != null && !hTmp.IsInvalid) { hTmp.Close(); } } }
private static void CreatePipeWithSecurityAttributes(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, Native.SECURITY_ATTRIBUTES lpPipeAttributes, int nSize) { bool ret = Native.CreatePipe(out hReadPipe, out hWritePipe, lpPipeAttributes, nSize); if (!ret || hReadPipe.IsInvalid || hWritePipe.IsInvalid) { throw new Win32Exception(); } }