Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ProcessManager.cs
Go to the documentation of this file.
5
6namespace System.Diagnostics;
7
8internal static class ProcessManager
9{
10 public static bool IsRemoteMachine(string machineName)
11 {
12 if (machineName == null)
13 {
14 throw new ArgumentNullException("machineName");
15 }
16 if (machineName.Length == 0)
17 {
18 throw new ArgumentException(System.SR.Format(System.SR.InvalidParameter, "machineName", machineName));
19 }
20 return IsRemoteMachineCore(machineName);
21 }
22
23 public static bool IsProcessRunning(int processId)
24 {
25 return IsProcessRunning(processId, ".");
26 }
27
28 public static bool IsProcessRunning(int processId, string machineName)
29 {
30 if (!IsRemoteMachine(machineName))
31 {
32 using SafeProcessHandle safeProcessHandle = global::Interop.Kernel32.OpenProcess(1024, inherit: false, processId);
33 if (!safeProcessHandle.IsInvalid)
34 {
35 return true;
36 }
37 }
38 return Array.IndexOf(GetProcessIds(machineName), processId) >= 0;
39 }
40
41 public static ProcessInfo[] GetProcessInfos(string machineName)
42 {
43 if (!IsRemoteMachine(machineName))
44 {
46 }
47 return NtProcessManager.GetProcessInfos(machineName, isRemoteMachine: true);
48 }
49
50 public static ProcessInfo GetProcessInfo(int processId, string machineName)
51 {
52 if (IsRemoteMachine(machineName))
53 {
54 ProcessInfo[] processInfos = NtProcessManager.GetProcessInfos(machineName, isRemoteMachine: true);
55 ProcessInfo[] array = processInfos;
56 foreach (ProcessInfo processInfo in array)
57 {
58 if (processInfo.ProcessId == processId)
59 {
60 return processInfo;
61 }
62 }
63 }
64 else
65 {
66 ProcessInfo[] processInfos2 = NtProcessInfoHelper.GetProcessInfos(processId);
67 if (processInfos2.Length == 1)
68 {
69 return processInfos2[0];
70 }
71 }
72 return null;
73 }
74
75 public static int[] GetProcessIds(string machineName)
76 {
77 if (!IsRemoteMachine(machineName))
78 {
79 return GetProcessIds();
80 }
81 return NtProcessManager.GetProcessIds(machineName, isRemoteMachine: true);
82 }
83
84 public static int[] GetProcessIds()
85 {
87 }
88
89 public static int GetProcessIdFromHandle(SafeProcessHandle processHandle)
90 {
91 return NtProcessManager.GetProcessIdFromHandle(processHandle);
92 }
93
94 public static ProcessModuleCollection GetModules(int processId)
95 {
96 return NtProcessManager.GetModules(processId);
97 }
98
99 private static bool IsRemoteMachineCore(string machineName)
100 {
101 ReadOnlySpan<char> span = machineName.AsSpan(machineName.StartsWith('\\') ? 2 : 0);
102 if (!MemoryExtensions.Equals(span, ".", StringComparison.Ordinal))
103 {
104 return !MemoryExtensions.Equals(span, global::Interop.Kernel32.GetComputerName(), StringComparison.OrdinalIgnoreCase);
105 }
106 return false;
107 }
108
109 unsafe static ProcessManager()
110 {
111 if (!global::Interop.Advapi32.LookupPrivilegeValue(null, "SeDebugPrivilege", out var lpLuid))
112 {
113 return;
114 }
115 SafeTokenHandle TokenHandle = null;
116 try
117 {
118 if (global::Interop.Advapi32.OpenProcessToken(global::Interop.Kernel32.GetCurrentProcess(), 32, out TokenHandle))
119 {
120 Unsafe.SkipInit(out global::Interop.Advapi32.TOKEN_PRIVILEGE tOKEN_PRIVILEGE);
121 tOKEN_PRIVILEGE.PrivilegeCount = 1u;
122 tOKEN_PRIVILEGE.Privileges.Luid = lpLuid;
123 tOKEN_PRIVILEGE.Privileges.Attributes = 2u;
124 global::Interop.Advapi32.AdjustTokenPrivileges(TokenHandle, DisableAllPrivileges: false, &tOKEN_PRIVILEGE, 0u, null, null);
125 }
126 }
127 finally
128 {
129 TokenHandle?.Dispose();
130 }
131 }
132
133 public static SafeProcessHandle OpenProcess(int processId, int access, bool throwIfExited)
134 {
135 SafeProcessHandle safeProcessHandle = global::Interop.Kernel32.OpenProcess(access, inherit: false, processId);
136 int lastWin32Error = Marshal.GetLastWin32Error();
137 if (!safeProcessHandle.IsInvalid)
138 {
139 return safeProcessHandle;
140 }
141 if (processId == 0)
142 {
143 throw new Win32Exception(5);
144 }
145 if (lastWin32Error != 5 && !IsProcessRunning(processId))
146 {
147 if (throwIfExited)
148 {
149 throw new InvalidOperationException(System.SR.Format(System.SR.ProcessHasExited, processId.ToString()));
150 }
152 }
153 throw new Win32Exception(lastWin32Error);
154 }
155
156 public static SafeThreadHandle OpenThread(int threadId, int access)
157 {
158 SafeThreadHandle safeThreadHandle = global::Interop.Kernel32.OpenThread(access, inherit: false, threadId);
159 int lastWin32Error = Marshal.GetLastWin32Error();
160 if (safeThreadHandle.IsInvalid)
161 {
162 if (lastWin32Error == 87)
163 {
164 throw new InvalidOperationException(System.SR.Format(System.SR.ThreadExited, threadId.ToString()));
165 }
166 throw new Win32Exception(lastWin32Error);
167 }
168 return safeThreadHandle;
169 }
170
171 public static IntPtr GetMainWindowHandle(int processId)
172 {
173 return MainWindowFinder.FindMainWindow(processId);
174 }
175}
static readonly SafeProcessHandle InvalidHandle
int IList. IndexOf(object value)
Definition Array.cs:1228
static unsafe ProcessInfo[] GetProcessInfos(int? processIdFilter=null)
static ProcessInfo[] GetProcessInfos(string machineName, bool isRemoteMachine)
static int[] GetProcessIds(string machineName, bool isRemoteMachine)
static int GetProcessIdFromHandle(SafeProcessHandle processHandle)
static ProcessModuleCollection GetModules(int processId)
static SafeProcessHandle OpenProcess(int processId, int access, bool throwIfExited)
static ProcessInfo[] GetProcessInfos(string machineName)
static int GetProcessIdFromHandle(SafeProcessHandle processHandle)
static bool IsProcessRunning(int processId)
static SafeThreadHandle OpenThread(int threadId, int access)
static IntPtr GetMainWindowHandle(int processId)
static bool IsRemoteMachine(string machineName)
static bool IsProcessRunning(int processId, string machineName)
static ProcessModuleCollection GetModules(int processId)
static int[] GetProcessIds(string machineName)
static ProcessInfo GetProcessInfo(int processId, string machineName)
static bool IsRemoteMachineCore(string machineName)
static bool Equals(this ReadOnlySpan< char > span, ReadOnlySpan< char > other, StringComparison comparisonType)
static string ProcessHasExited
Definition SR.cs:32
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ThreadExited
Definition SR.cs:36
static string InvalidParameter
Definition SR.cs:62
Definition SR.cs:7
static unsafe IntPtr FindMainWindow(int processId)