public static SafeNativeHandle OpenProcess(Int32 pid, ProcessAccessFlags access, bool inherit) { SafeNativeHandle hProcess = NativeMethods.OpenProcess(access, inherit, (UInt32)pid); if (hProcess.IsInvalid) { throw new Win32Exception(String.Format("Failed to open process {0} with access {1}", pid, access.ToString())); } return(hProcess); }
public static SafeMemoryHandle OpenProcess(int pId, ProcessAccessFlags accessRights = ProcessAccessFlags.PROCESS_ALL_ACCESS) { SafeMemoryHandle processHandle = Imports.OpenProcess(accessRights, false, pId); if (processHandle == null || processHandle.IsInvalid || processHandle.IsClosed) { throw new Win32Exception($"[Win32 Error: {Marshal.GetLastWin32Error()}] " + $"Unable to open process {pId} with access {accessRights.ToString("X")}"); } return(processHandle); }
public static TMProcessHandle FromProcessId(int pid, ProcessAccessFlags desiredAccess = ProcessAccessFlags.All, bool inheritHandle = true) { IntPtr hProcess = Kernel32.OpenProcess(desiredAccess, inheritHandle, pid); if (hProcess == IntPtr.Zero) { string errMsg = $"Failed to open handle to process '{pid}' with the access flag '{desiredAccess.ToString()}'. OpenProcess failed with error code: {Kernel32.GetLastError()}."; Logger.GetInstance().Error(errMsg); throw new OpenProcessException(errMsg); } return(new TMProcessHandle(hProcess, desiredAccess, false)); }