/// <summary> /// Duplicates a given handle owned by a given process into a given destination process /// </summary> /// <param name="owner">A handle to the process that owns the handle</param> /// <param name="sourceHandle">The handle to be duplicated</param> /// <param name="desiredRights">The desired rights of the new handle. Implementation specific</param> /// <param name="inherit">Whether the new handle is inheritable</param> /// <param name="options">The duplication options to use when duplicating</param> /// <param name="destination">The process that should recieve the handle duplicate</param> /// <returns>The newly duplicated handle</returns> /// <exception cref="Win32Exception">On windows api error</exception> public static IntPtr Duplicate(SafeProcessHandle owner, IntPtr sourceHandle, UInt32 desiredRights, Boolean inherit, DuplicationOptions options, SafeProcessHandle destination) { IntPtr destinationHandle = IntPtr.Zero; if (!Kernel32.DuplicateHandle(owner.DangerousGetHandle(), sourceHandle, destination.DangerousGetHandle(), ref destinationHandle, desiredRights, inherit, options)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not duplicate handle"); } return(destinationHandle); }
/// <summary> /// Duplicates a given process handle owned by a given process into a given destination process /// </summary> /// <param name="owner">A handle to the process that owns the handle</param> /// <param name="sourceHandle">The process handle to be duplicated</param> /// <param name="desiredRights">The desired process rights of the new handle</param> /// <param name="inherit">Whether the new handle is inheritable</param> /// <param name="options">The duplication options to use when duplicating</param> /// <param name="destination">The process that should recieve the handle duplicate</param> /// <returns>The newly duplicated handle</returns> /// <exception cref="Win32Exception">On windows api error</exception> public static SafeProcessHandle DuplicateProcessHandle(SafeProcessHandle owner, IntPtr sourceHandle, ProcessRights desiredRights, Boolean inherit, DuplicationOptions options, SafeProcessHandle destination) { IntPtr handle = Duplicate(owner, sourceHandle, (UInt32)desiredRights, inherit, options, destination); return(new SafeProcessHandle(handle, true)); }
/// <summary> /// Duplicates a given process handle owned by a given process into this process /// </summary> /// <param name="owner">A handle to the process that owns the handle</param> /// <param name="sourceHandle">The process handle to be duplicated</param> /// <param name="desiredRights">The desired process rights of the new handle</param> /// <param name="inherit">Whether the new handle is inheritable</param> /// <param name="options">The duplication options to use when duplicating</param> /// <returns>The newly duplicated handle</returns> /// <exception cref="Win32Exception">On windows api error</exception> public static SafeProcessHandle DuplicateProcessHandle(SafeProcessHandle owner, IntPtr sourceHandle, ProcessRights desiredRights, Boolean inherit, DuplicationOptions options) { var process = Process.GetCurrentProcess(); return(DuplicateProcessHandle(owner, sourceHandle, desiredRights, inherit, options, process.Handle)); }
public static extern Boolean DuplicateHandle([In] IntPtr sourceProcessHandle, [In] IntPtr sourceHandle, [In] IntPtr targetProcessHandle, ref IntPtr targetHandle, [In] UInt32 desiredRights, [In] Boolean inherit, [In] DuplicationOptions duplicationOptions);