Skip to content

Commit d0d6cc9

Browse files
committed
LibraryImport
1 parent aef2b37 commit d0d6cc9

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

src/DiffEngine/DiffEngine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT'">net462;net472;net48;net9.0;net10.0</TargetFrameworks>
44
<TargetFrameworks>$(TargetFrameworks);net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
5+
<AllowUnsafeBlocks Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</AllowUnsafeBlocks>
56
</PropertyGroup>
67
<ItemGroup>
78
<Using Include="DiffEngine" />

src/DiffEngine/Process/WindowsProcess.cs

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,63 @@
1-
static class WindowsProcess
1+
using System.Text;
2+
3+
static partial class WindowsProcess
24
{
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)]
455
static extern SafeProcessHandle OpenProcess(
556
int access,
657
bool inherit,
758
int processId);
859

9-
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
60+
[DllImport("kernel32.dll", SetLastError = true)]
1061
static extern bool TerminateProcess(
1162
SafeProcessHandle processHandle,
1263
int exitCode);
@@ -41,6 +92,7 @@ static extern bool ReadProcessMemory(
4192

4293
[DllImport("kernel32.dll", SetLastError = true)]
4394
static extern bool IsWow64Process(SafeProcessHandle handle, out bool isWow64);
95+
#endif
4496

4597
const uint TH32CS_SNAPPROCESS = 0x00000002;
4698
const int PROCESS_QUERY_INFORMATION = 0x0400;
@@ -75,14 +127,6 @@ struct PROCESS_BASIC_INFORMATION
75127
public IntPtr Reserved3;
76128
}
77129

78-
[StructLayout(LayoutKind.Sequential)]
79-
struct UNICODE_STRING
80-
{
81-
public ushort Length;
82-
public ushort MaximumLength;
83-
public IntPtr Buffer;
84-
}
85-
86130
public static bool TryTerminateProcess(int processId)
87131
{
88132
using var processHandle = OpenProcess(PROCESS_TERMINATE, false, processId);

0 commit comments

Comments
 (0)