public ProcessDetail(int pid) { // Initialize everything to null in case something fails. this.processId = pid; this.processHandleFlags = LowLevelTypes.ProcessAccessFlags.NONE; this.cachedProcessBasicInfo = null; this.machineTypeIsLoaded = false; this.machineType = LowLevelTypes.MachineType.UNKNOWN; this.cachedPeb = null; this.cachedProcessParams = null; this.cachedCommandLine = null; this.processHandle = IntPtr.Zero; OpenAndCacheProcessHandle(); }
private void OpenAndCacheProcessHandle() { // Try to open a handle to the process with the highest level of privilege, but if we can't // do that then fallback to requesting access with a lower privilege level. processHandleFlags = LowLevelTypes.ProcessAccessFlags.QUERY_INFORMATION | LowLevelTypes.ProcessAccessFlags.VM_READ; processHandle = NativeMethods.OpenProcess(processHandleFlags, false, processId); if (processHandle == IntPtr.Zero) { processHandleFlags = LowLevelTypes.ProcessAccessFlags.QUERY_LIMITED_INFORMATION; processHandle = NativeMethods.OpenProcess(processHandleFlags, false, processId); if (processHandle == IntPtr.Zero) { processHandleFlags = LowLevelTypes.ProcessAccessFlags.NONE; throw new Win32Exception(); } } }
public static extern IntPtr OpenProcess( LowLevelTypes.ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);