/// <summary> /// 创建输入输入重定向通道 /// </summary> /// <param name="parentHandle">父进程句柄</param> /// <param name="childHandle">子进程句柄</param> /// <param name="parentInputs">是否输入</param> private void createPipe(out SafeFileHandle parentHandle, out SafeFileHandle childHandle, bool parentInputs) { kernel32.securityAttributes lpPipeAttributes = new kernel32.securityAttributes(); lpPipeAttributes.IsInheritHandle = true; SafeFileHandle hWritePipe = null; try { if (parentInputs) { createPipeWithSecurityAttributes(out childHandle, out hWritePipe, lpPipeAttributes, 0); } else { createPipeWithSecurityAttributes(out hWritePipe, out childHandle, lpPipeAttributes, 0); } if (!kernel32.DuplicateHandle(new HandleRef(this, kernel32.GetCurrentProcess()), hWritePipe, new HandleRef(this, kernel32.GetCurrentProcess()), out parentHandle, 0, false, kernel32.duplicateHandleOptions.DUPLICATE_SAME_ACCESS)) { throw new Win32Exception(); } } finally { if ((hWritePipe != null) && !hWritePipe.IsInvalid) { hWritePipe.Close(); } } }
/// <summary> /// 创建通道 /// </summary> /// <param name="hReadPipe">读取通道</param> /// <param name="hWritePipe">写入通道</param> /// <param name="lpPipeAttributes">安全属性</param> /// <param name="nSize">缓存区字节数,0表示默认</param> private static void createPipeWithSecurityAttributes(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe, kernel32.securityAttributes lpPipeAttributes, int nSize) { if ((!kernel32.CreatePipe(out hReadPipe, out hWritePipe, lpPipeAttributes, nSize) || hReadPipe.IsInvalid) || hWritePipe.IsInvalid) { throw new Win32Exception(); } }