Skip to content

Commit 7d49548

Browse files
committed
refactor: move SYSTEM_HANDLE structs to new files
1 parent 97e4992 commit 7d49548

File tree

3 files changed

+441
-133
lines changed

3 files changed

+441
-133
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Re: StructLayout
2+
// "C#, Visual Basic, and C++ compilers apply the Sequential layout value to structures by default."
3+
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.structlayoutattribute?view=net-6.0#remarks
4+
5+
// new Win32Exception() is defined as
6+
// public Win32Exception() : this(Marshal.GetLastPInvokeError())
7+
// {
8+
// }
9+
10+
namespace deadlock_dotnet_sdk.Domain;
11+
12+
internal static partial class NativeMethods
13+
{
14+
//private static extern void MmIsAddressValid()
15+
/// <summary>
16+
/// The <see href="https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/ex/sysinfo/handle_ex.htm"><c>SYSTEM_HANDLE_INFORMATION_EX</c></see>
17+
/// struct is 0x24 or 0x38 bytes in 32-bit and 64-bit Windows, respectively. However, Handles is a variable-length array.
18+
/// </summary>
19+
public unsafe struct SYSTEM_HANDLE_INFORMATION_EX
20+
{
21+
#pragma warning disable CS0649
22+
23+
/// <summary>
24+
/// As documented unofficially, NumberOfHandles is a 4-byte or 8-byte ULONG_PTR in 32-bit and 64-bit Windows, respectively.<br/>
25+
/// This is not to be confused with uint* or ulong*.
26+
/// </summary>
27+
public readonly UIntPtr NumberOfHandles;
28+
public readonly UIntPtr Reserved;
29+
public readonly SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX Handle_0;
30+
31+
/// <summary>
32+
/// If IsEmpty is true, AsSpan() failed.
33+
/// </summary>
34+
/// <value></value>
35+
public ReadOnlySpan<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> ReadOnlySpan
36+
{
37+
get
38+
{
39+
try
40+
{
41+
return AsSpan();
42+
}
43+
catch (Exception)
44+
{
45+
return ReadOnlySpan<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX>.Empty;
46+
}
47+
}
48+
}
49+
#pragma warning restore CS0649
50+
51+
/// <summary>
52+
/// Infer an array from the address of Handle_0 and NumberOfHandles, then return it as a ReadOnlySpan
53+
/// </summary>
54+
/// <exception cref="ArgumentException"/>
55+
/// <exception cref="ArgumentOutOfRangeException"/>
56+
public ReadOnlySpan<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> AsSpan()
57+
{
58+
fixed (SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX* pHandle_0 = &Handle_0)
59+
return new ReadOnlySpan<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX>(pHandle_0, (int)NumberOfHandles).ToArray();
60+
}
61+
62+
/// <summary>
63+
/// DEBUGGING | Test for memory access. System.AccessViolationException due to these values being in a protected memory range is a problem.
64+
/// </summary>
65+
internal void CheckAccess()
66+
{
67+
var tmp = AsSpan();
68+
69+
var lastItem = tmp[(int)NumberOfHandles - 1];
70+
71+
Console.WriteLine(lastItem + ": " + lastItem.UniqueProcessId);
72+
}
73+
74+
public static explicit operator ReadOnlySpan<SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX>(SYSTEM_HANDLE_INFORMATION_EX value) => value.AsSpan();
75+
}
76+
}

0 commit comments

Comments
 (0)