|
1 | | -static class WindowsProcess |
| 1 | +using System.Text; |
| 2 | + |
| 3 | +static partial class WindowsProcess |
2 | 4 | { |
3 | | - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
| 5 | +#if NET7_0_OR_GREATER |
| 6 | + [LibraryImport("kernel32.dll", SetLastError = true)] |
| 7 | + private static partial SafeProcessHandle OpenProcess( |
| 8 | + int access, |
| 9 | + [MarshalAs(UnmanagedType.Bool)] bool inherit, |
| 10 | + int processId); |
| 11 | + |
| 12 | + [LibraryImport("kernel32.dll", SetLastError = true)] |
| 13 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 14 | + private static partial bool TerminateProcess( |
| 15 | + SafeProcessHandle processHandle, |
| 16 | + int exitCode); |
| 17 | + |
| 18 | + [LibraryImport("kernel32.dll", SetLastError = true)] |
| 19 | + private static partial IntPtr CreateToolhelp32Snapshot(uint flags, uint processId); |
| 20 | + |
| 21 | + [LibraryImport("kernel32.dll", SetLastError = true)] |
| 22 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 23 | + private static partial bool CloseHandle(IntPtr handle); |
| 24 | + |
| 25 | + [LibraryImport("ntdll.dll")] |
| 26 | + private static partial int NtQueryInformationProcess( |
| 27 | + SafeProcessHandle handle, |
| 28 | + int processInformationClass, |
| 29 | + ref PROCESS_BASIC_INFORMATION info, |
| 30 | + int size, |
| 31 | + out int returnLength); |
| 32 | + |
| 33 | + [LibraryImport("kernel32.dll", SetLastError = true)] |
| 34 | + [return: MarshalAs(UnmanagedType.Bool)] |
| 35 | + private static partial bool IsWow64Process( |
| 36 | + SafeProcessHandle handle, |
| 37 | + [MarshalAs(UnmanagedType.Bool)] out bool isWow64); |
| 38 | + |
| 39 | + // These methods use complex marshalling not supported by LibraryImport |
| 40 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 41 | + static extern bool Process32FirstW(IntPtr snapshot, ref PROCESSENTRY32W entry); |
| 42 | + |
| 43 | + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] |
| 44 | + static extern bool Process32NextW(IntPtr snapshot, ref PROCESSENTRY32W entry); |
| 45 | + |
| 46 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 47 | + static extern bool ReadProcessMemory( |
| 48 | + SafeProcessHandle handle, |
| 49 | + IntPtr baseAddress, |
| 50 | + [Out] byte[] buffer, |
| 51 | + IntPtr size, |
| 52 | + out IntPtr bytesRead); |
| 53 | +#else |
| 54 | + [DllImport("kernel32.dll", SetLastError = true)] |
4 | 55 | static extern SafeProcessHandle OpenProcess( |
5 | 56 | int access, |
6 | 57 | bool inherit, |
7 | 58 | int processId); |
8 | 59 |
|
9 | | - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] |
| 60 | + [DllImport("kernel32.dll", SetLastError = true)] |
10 | 61 | static extern bool TerminateProcess( |
11 | 62 | SafeProcessHandle processHandle, |
12 | 63 | int exitCode); |
@@ -41,6 +92,7 @@ static extern bool ReadProcessMemory( |
41 | 92 |
|
42 | 93 | [DllImport("kernel32.dll", SetLastError = true)] |
43 | 94 | static extern bool IsWow64Process(SafeProcessHandle handle, out bool isWow64); |
| 95 | +#endif |
44 | 96 |
|
45 | 97 | const uint TH32CS_SNAPPROCESS = 0x00000002; |
46 | 98 | const int PROCESS_QUERY_INFORMATION = 0x0400; |
@@ -75,14 +127,6 @@ struct PROCESS_BASIC_INFORMATION |
75 | 127 | public IntPtr Reserved3; |
76 | 128 | } |
77 | 129 |
|
78 | | - [StructLayout(LayoutKind.Sequential)] |
79 | | - struct UNICODE_STRING |
80 | | - { |
81 | | - public ushort Length; |
82 | | - public ushort MaximumLength; |
83 | | - public IntPtr Buffer; |
84 | | - } |
85 | | - |
86 | 130 | public static bool TryTerminateProcess(int processId) |
87 | 131 | { |
88 | 132 | using var processHandle = OpenProcess(PROCESS_TERMINATE, false, processId); |
|
0 commit comments