public static SafeMemoryHandle OpenProcess(ProcessAccessFlags accessFlags, int processId) { // Get an handle from the remote process var handle = NativeMethods.OpenProcess(accessFlags, false, processId); // Check whether the handle is valid if (!handle.IsInvalid && !handle.IsClosed) return handle; // Else the handle isn't valid, throws an exception throw new Win32Exception(string.Format("Couldn't open the process {0}.", processId)); }