Skip to content

Commit 41b4c5a

Browse files
committed
refactor: if PID == 4, don't open process handle
1 parent 572ce32 commit 41b4c5a

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

deadlock-dotnet-sdk/Domain/SafeHandleEx.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,31 @@ internal SafeHandleEx(NativeMethods.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX sysHandleE
5050
{
5151
ProcessName = "System";
5252
}
53+
else
54+
{
55+
HANDLE rawHandle = OpenProcess(
56+
dwDesiredAccess: PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_ACCESS_RIGHTS.PROCESS_VM_READ,
57+
bInheritHandle: (BOOL)false,
58+
dwProcessId: ProcessId
59+
);
5360

54-
HANDLE rawHandle = OpenProcess(
55-
dwDesiredAccess: PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_ACCESS_RIGHTS.PROCESS_VM_READ,
56-
bInheritHandle: (BOOL)false,
57-
dwProcessId: ProcessId
58-
);
61+
if (rawHandle.IsNull)
62+
throw new Win32Exception("Failed to open process handle with access rights 'PROCESS_QUERY_LIMITED_INFORMATION' and 'PROCESS_VM_READ'. The following information will be unavailable: main module full name, process name, process' startup command line");
5963

60-
if (rawHandle.IsNull)
61-
throw new Win32Exception("Failed to open process handle with access rights 'PROCESS_QUERY_LIMITED_INFORMATION' and 'PROCESS_VM_READ'. The following information will be unavailable: main module full name, process name, process' startup command line");
64+
using SafeProcessHandle hProcess = new(rawHandle, true);
6265

63-
using SafeProcessHandle hProcess = new(rawHandle, true);
66+
/** Get main module's full path */
67+
ProcessMainModulePath = GetFullProcessImageName(hProcess);
6468

65-
/** Get main module's full path */
66-
ProcessMainModulePath = GetFullProcessImageName(hProcess);
69+
/** Get Process's name */
70+
if (!string.IsNullOrWhiteSpace(ProcessMainModulePath))
71+
{
72+
ProcessName = Path.GetFileNameWithoutExtension(ProcessMainModulePath);
73+
}
6774

68-
/** Get Process's name */
69-
if (!string.IsNullOrWhiteSpace(ProcessMainModulePath))
70-
{
71-
ProcessName = Path.GetFileNameWithoutExtension(ProcessMainModulePath);
75+
/** Get process's possibly-overwritten command line from the PEB struct in its memory space */
76+
ProcessCommandLine = GetProcessCommandLine(hProcess);
7277
}
73-
74-
/** Get process's possibly-overwritten command line from the PEB struct in its memory space */
75-
ProcessCommandLine = GetProcessCommandLine(hProcess);
7678
}
7779
catch (Exception e)
7880
{

0 commit comments

Comments
 (0)