private static bool CreatePrimaryToken(IntPtr impersonationToken, out IntPtr primaryToken) { // Convert the impersonation token into Primary token NativeMethods.SecurityAttributes sa = new NativeMethods.SecurityAttributes(); bool retVal = NativeMethods.DuplicateTokenEx( impersonationToken, NativeMethods.TokenAccess.AssignPrimary | NativeMethods.TokenAccess.Duplicate| NativeMethods.TokenAccess.Query, sa, NativeMethods.SecurityImpersonationLevel.Identification, NativeMethods.TokenType.Primary, out primaryToken); // Close the Token that was previously opened. NativeMethods.CloseHandle(impersonationToken); return retVal; }
private void CreateStandardPipe(out SafeFileHandle readHandle, out SafeFileHandle writeHandle, int standardHandle, bool isInput, bool redirect) { if (redirect) { NativeMethods.SecurityAttributes security = new NativeMethods.SecurityAttributes { bInheritHandle = true }; bool success = NativeMethods.CreatePipe(out readHandle, out writeHandle, security, 4096); if (success) success = NativeMethods.SetHandleInformation(isInput ? writeHandle : readHandle, HANDLE_FLAG_INHERIT, 0); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error(), "ImpersonationProcess: could not create standard pipe"); } else { if (isInput) { writeHandle = new SafeFileHandle(IntPtr.Zero, false); readHandle = NativeMethods.GetStdHandle(standardHandle); } else { readHandle = new SafeFileHandle(IntPtr.Zero, false); writeHandle = NativeMethods.GetStdHandle(standardHandle); } } }