Skip to content

Commit 79397ec

Browse files
committed
refactor: move CsWin32 supplements to new files
todo: move SYSTEM_HANDLE_ definitions to CsWin32 namespace
1 parent d59487b commit 79397ec

17 files changed

+746
-238
lines changed

deadlock-dotnet-sdk/CsWin32_NativeMethods.cs

Lines changed: 0 additions & 237 deletions
This file was deleted.

deadlock-dotnet-sdk/NativeMethods.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,29 @@ DUPLICATE_HANDLE_OPTIONS
2424
RM_PROCESS_INFO
2525

2626
//// PInvoke ////
27+
CheckTokenMembership
2728
CloseHandle
2829
DuplicateHandle
2930
GetFinalPathNameByHandle
3031
IsWow64Process
31-
NtQueryObject
3232
NtQueryInformationProcess
33+
NtQueryObject
3334
NtQuerySystemInformation
3435
OpenProcess
36+
OpenProcessToken
37+
OpenThread
38+
OpenThreadToken
3539
QueryFullProcessImageName
3640
ReadProcessMemory
3741
RmEndSession
3842
RmGetList
3943
RmRegisterResources
4044
RmStartSession
4145

46+
//// System.Memory ////
47+
MEMORY_BASIC_INFORMATION32
48+
MEMORY_BASIC_INFORMATION64
49+
4250
//// System.Threading ////
4351
PEB
4452
PROCESS_BASIC_INFORMATION
@@ -47,3 +55,14 @@ PROCESSINFOCLASS
4755
//// System.WindowsProgramming ////
4856
PUBLIC_OBJECT_TYPE_INFORMATION
4957

58+
//// does not exist in Win32Metadata ////
59+
//LPCVOID
60+
//SIZE_T
61+
//OBJECT_TYPES_INFORMATION
62+
//OBJECT_TYPE_INFORMATION
63+
64+
//// PInvoke005: This API is only available when targeting a specific CPU architecture. AnyCPU cannot generate this API. ////
65+
//MEMORY_BASIC_INFORMATION
66+
//VirtualQuery
67+
68+
GENERIC_MAPPING
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// This file supplements code generated by CsWin32
2+
using Win32Exception = System.ComponentModel.Win32Exception;
3+
4+
namespace Windows.Win32.Foundation;
5+
6+
partial struct HANDLE : IComparable<HANDLE>
7+
{
8+
public static implicit operator HANDLE(nuint v) => new((nint)v);
9+
public static implicit operator nuint(HANDLE v) => (nuint)(nint)v.Value;
10+
11+
public static implicit operator HANDLE(nint v) => new(v);
12+
public static implicit operator nint(HANDLE v) => v.Value;
13+
14+
/// <summary>
15+
/// Close the handle via the CloseHandle function
16+
/// </summary>
17+
/// <exception cref="Win32Exception">
18+
/// If the application is running under a debugger, the function will throw an
19+
/// exception if it receives either a handle value that is not valid or a
20+
/// pseudo-handle value. This can happen if you close a handle twice, or if you
21+
/// call CloseHandle on a handle returned by the FindFirstFile function instead
22+
/// of calling the FindClose function.
23+
/// </exception>
24+
public void Close()
25+
{
26+
if (!PInvoke.CloseHandle(this))
27+
throw new Win32Exception();
28+
}
29+
30+
public int CompareTo(HANDLE other) => Value.CompareTo(other);
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// This file supplements code generated by CsWin32
2+
using NTStatusException = PInvoke.NTStatusException;
3+
4+
namespace Windows.Win32.Foundation;
5+
6+
readonly partial struct NTSTATUS
7+
{
8+
public bool IsSuccessful => SeverityCode == Severity.Success;
9+
10+
public NTStatusException GetNTStatusException() => new(this);
11+
12+
public static implicit operator global::PInvoke.NTSTATUS(NTSTATUS v) => new(v.Value);
13+
public static implicit operator NTSTATUS(global::PInvoke.NTSTATUS v) => new(v.AsInt32);
14+
15+
public static implicit operator global::PInvoke.NTSTATUS.Code(NTSTATUS v) => ((global::PInvoke.NTSTATUS)v).Value;
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// This file supplements code generated by CsWin32
2+
using System.Runtime.InteropServices;
3+
4+
namespace Windows.Win32.Foundation;
5+
6+
unsafe readonly partial struct PWSTR : IDisposable
7+
{
8+
/// <summary>
9+
/// Free the PWSTR's memory with Marshal.FreeHGlobal(IntPtr)
10+
/// </summary>
11+
public void Dispose() => Marshal.FreeHGlobal((IntPtr)Value);
12+
13+
public static implicit operator PWSTR(IntPtr v) => new((char*)v);
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// This file supplements code generated by CsWin32
2+
using System.Runtime.InteropServices;
3+
4+
namespace Windows.Win32.Foundation;
5+
6+
partial struct UNICODE_STRING : IDisposable
7+
{
8+
public void Dispose()
9+
{
10+
Buffer.Dispose();
11+
}
12+
13+
/// <summary>
14+
/// Allocates a managed string and copies a specified number of characters from an unmanaged Unicode string into it.
15+
/// </summary>
16+
public unsafe string ToStringLength() => Marshal.PtrToStringUni((IntPtr)Buffer.Value, Length / 2);
17+
public string? ToStringZ() => Buffer.ToString();
18+
public static explicit operator string(UNICODE_STRING v) => v.ToStringLength();
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using static PInvoke.Kernel32;
2+
3+
namespace Windows.Win32;
4+
public struct GENERIC_MAPPING
5+
{
6+
public ACCESS_MASK GenericRead;
7+
public ACCESS_MASK GenericWrite;
8+
public ACCESS_MASK GenericExecute;
9+
public ACCESS_MASK GenericAll;
10+
}

0 commit comments

Comments
 (0)