public ProcessExtended(int processId) { Contract.Ensures(Process != null); Contract.Ensures(_processHandle != null); Process = Process.GetProcessById(processId); _processHandle = new SafeTokenHandle(Process.Handle, false); _initializedByProcessHandle = false; ResetCpuUsage(); }
public static bool WaitForSingleObject(SafeTokenHandle tokenHandle, uint milliseconds = InfiniteTimeout) { Contract.Requires<ArgumentNullException>(tokenHandle != null, TokenHandleExceptionMessage); var waitResult = (WaitResult) WaitForSingleObject(tokenHandle.DangerousGetHandle(), milliseconds); if (waitResult == WaitResult.Failed) { WindowsApi.NativeMethods.ReportWin32Exception(); } return waitResult != WaitResult.Timeout; }
public static SafeTokenHandle DuplicateTokenEx(SafeTokenHandle existingToken, System.Security.Principal.TokenAccessLevels desiredAccess, System.Security.Principal.TokenImpersonationLevel impersonationLevel, TokenType tokenType) { Contract.Requires<ArgumentNullException>(existingToken != null, ExistingTokenExceptionMessage); IntPtr token; if (!DuplicateTokenEx(existingToken.DangerousGetHandle(), (uint) desiredAccess, IntPtr.Zero, impersonationLevel, tokenType, out token)) { WindowsApi.NativeMethods.ReportWin32Exception(); } return new SafeTokenHandle(token); }
/// <summary> /// Initialize constructor. /// </summary> /// <param name="processHandle">process handle</param> public ProcessExtended(SafeTokenHandle processHandle) { Contract.Requires<ArgumentNullException>(processHandle != null, "processHandle cannot be null"); Contract.Ensures(Process != null); Contract.Ensures(_processHandle != null); var handle = processHandle.DangerousGetHandle(); Process = Process.GetProcesses().Single(p => p.Id != 0 && p.Handle == handle); _processHandle = processHandle; _initializedByProcessHandle = true; }
public ProcessExtended(Process process) { Contract.Requires<ArgumentNullException>(process != null, "process cannot be null"); Contract.Ensures(Process != null); Contract.Ensures(_processHandle != null); Process = process; _processHandle = new SafeTokenHandle(Process.Handle, false); _initializedByProcessHandle = false; ResetCpuUsage(); }
public static void AdjustTokenPrivileges(SafeTokenHandle tokenHandle, bool disableAllPrivileges, ref TokenPrivileges newState) { Contract.Requires<ArgumentNullException>(tokenHandle != null, "tokenHandle cannot be null"); if (!AdjustTokenPrivileges(tokenHandle.DangerousGetHandle(), disableAllPrivileges, ref newState, 0u, IntPtr.Zero, IntPtr.Zero)) { WindowsApi.NativeMethods.ReportWin32Exception(); } var error = Marshal.GetLastWin32Error(); if (error == ErrorCodes.NotAllAssigned) { try { throw new PrivilegeNotHeldException(GetPrivilegeName(PrivilegeFromLuid(newState.Privileges[0].Luid))); } catch (InvalidOperationException) { throw new PrivilegeNotHeldException(); } } }
public static void ImpersonateLoggedOnUser(SafeTokenHandle tokenHandle) { Contract.Requires<ArgumentNullException>(tokenHandle != null, TokenExceptionMessage); if (!ImpersonateLoggedOnUser(tokenHandle.DangerousGetHandle())) { WindowsApi.NativeMethods.ReportWin32Exception(); } }