1+ //********************************************************************************************************************************
2+ //https://github.com/gapotchenko/Gapotchenko.FX/blob/master/Source/Gapotchenko.FX.Diagnostics.Process/Pal/Windows/NativeMethods.cs
3+ //********************************************************************************************************************************
4+ using System ;
5+ using System . Runtime . InteropServices ;
6+ using System . Text ;
7+
8+ namespace WindowTextExtractor . Diagnostics
9+ {
10+ static class NativeMethods
11+ {
12+ [ StructLayout ( LayoutKind . Sequential ) ]
13+ public struct SYSTEM_INFO
14+ {
15+ public ushort wProcessorArchitecture ;
16+ public ushort wReserved ;
17+ public uint dwPageSize ;
18+ public IntPtr lpMinimumApplicationAddress ;
19+ public IntPtr lpMaximumApplicationAddress ;
20+ public IntPtr dwActiveProcessorMask ;
21+ public uint dwNumberOfProcessors ;
22+ public uint dwProcessorType ;
23+ public uint dwAllocationGranularity ;
24+ public ushort wProcessorLevel ;
25+ public ushort wProcessorRevision ;
26+ }
27+
28+ [ DllImport ( "kernel32.dll" , ExactSpelling = true ) ]
29+ public static extern void GetSystemInfo ( out SYSTEM_INFO lpSystemInfo ) ;
30+
31+ [ DllImport ( "kernel32.dll" , ExactSpelling = true ) ]
32+ public static extern void GetNativeSystemInfo ( out SYSTEM_INFO lpSystemInfo ) ;
33+
34+ // -------------------------------------------------------------------------------------------------------
35+
36+ [ StructLayout ( LayoutKind . Sequential , Pack = 1 ) ]
37+ public struct PROCESS_BASIC_INFORMATION
38+ {
39+ public IntPtr Reserved1 ;
40+ public IntPtr PebBaseAddress ;
41+ [ MarshalAs ( UnmanagedType . ByValArray , SizeConst = 2 ) ]
42+ public IntPtr [ ] Reserved2 ;
43+ public IntPtr UniqueProcessId ;
44+ public IntPtr InheritedFromUniqueProcessId ;
45+ }
46+
47+ public const int ProcessBasicInformation = 0 ;
48+ public const int ProcessWow64Information = 26 ;
49+
50+ [ DllImport ( "ntdll.dll" , SetLastError = true ) ]
51+ public static extern int NtQueryInformationProcess (
52+ IntPtr hProcess ,
53+ int pic ,
54+ ref PROCESS_BASIC_INFORMATION pbi ,
55+ int cb ,
56+ ref int pSize ) ;
57+
58+ [ DllImport ( "ntdll.dll" , SetLastError = true ) ]
59+ public static extern int NtQueryInformationProcess (
60+ IntPtr hProcess ,
61+ int pic ,
62+ ref IntPtr pi ,
63+ int cb ,
64+ ref int pSize ) ;
65+
66+ [ StructLayout ( LayoutKind . Sequential , Pack = 1 ) ]
67+ public struct PROCESS_BASIC_INFORMATION_WOW64
68+ {
69+ public long Reserved1 ;
70+ public long PebBaseAddress ;
71+ [ MarshalAs ( UnmanagedType . ByValArray , SizeConst = 2 ) ]
72+ public long [ ] Reserved2 ;
73+ public long UniqueProcessId ;
74+ public long Reserved3 ;
75+ }
76+
77+ [ DllImport ( "ntdll.dll" , SetLastError = true ) ]
78+ public static extern int NtWow64QueryInformationProcess64 (
79+ IntPtr hProcess ,
80+ int pic ,
81+ ref PROCESS_BASIC_INFORMATION_WOW64 pbi ,
82+ int cb ,
83+ ref int pSize ) ;
84+
85+ [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
86+ public static unsafe extern bool ReadProcessMemory (
87+ IntPtr hProcess ,
88+ IntPtr lpBaseAddress ,
89+ void * lpBuffer ,
90+ int dwSize ,
91+ ref int lpNumberOfBytesRead ) ;
92+
93+ [ DllImport ( "ntdll.dll" , SetLastError = true ) ]
94+ public static unsafe extern int NtWow64ReadVirtualMemory64 (
95+ IntPtr hProcess ,
96+ long lpBaseAddress ,
97+ void * lpBuffer ,
98+ long dwSize ,
99+ ref long lpNumberOfBytesRead ) ;
100+
101+ public const int STATUS_SUCCESS = 0 ;
102+
103+ public const int PAGE_NOACCESS = 0x01 ;
104+ public const int PAGE_EXECUTE = 0x10 ;
105+
106+ [ StructLayout ( LayoutKind . Sequential ) ]
107+ public struct MEMORY_BASIC_INFORMATION
108+ {
109+ public IntPtr BaseAddress ;
110+ public IntPtr AllocationBase ;
111+ public int AllocationProtect ;
112+ public IntPtr RegionSize ;
113+ public int State ;
114+ public int Protect ;
115+ public int Type ;
116+ }
117+
118+ [ DllImport ( "kernel32.dll" ) ]
119+ public static extern int VirtualQueryEx ( IntPtr hProcess , IntPtr lpAddress , ref MEMORY_BASIC_INFORMATION lpBuffer , int dwLength ) ;
120+
121+ [ StructLayout ( LayoutKind . Sequential ) ]
122+ public struct MEMORY_BASIC_INFORMATION_WOW64
123+ {
124+ public long BaseAddress ;
125+ public long AllocationBase ;
126+ public int AllocationProtect ;
127+ public long RegionSize ;
128+ public int State ;
129+ public int Protect ;
130+ public int Type ;
131+ }
132+
133+ public enum MEMORY_INFORMATION_CLASS
134+ {
135+ MemoryBasicInformation
136+ }
137+
138+ [ DllImport ( "ntdll.dll" ) ]
139+ public static extern int NtWow64QueryVirtualMemory64 (
140+ IntPtr hProcess ,
141+ long lpAddress ,
142+ MEMORY_INFORMATION_CLASS memoryInformationClass ,
143+ IntPtr lpBuffer , // MEMORY_BASIC_INFORMATION_WOW64, pointer must be 64-bit aligned
144+ long memoryInformationLength ,
145+ ref long returnLength ) ;
146+
147+ [ DllImport ( "kernel32.dll" ) ]
148+ public static extern bool IsWow64Process ( IntPtr hProcess , out bool wow64Process ) ;
149+
150+ // -------------------------------------------------------------------------------------------------------
151+
152+ public const int ERROR_ACCESS_DENIED = 5 ;
153+ public const int ERROR_INVALID_HANDLE = 6 ;
154+ public const int ERROR_INVALID_PARAMETER = 87 ;
155+
156+ public const int CTRL_C_EVENT = 0 ;
157+ public const int CTRL_BREAK_EVENT = 1 ;
158+
159+ [ DllImport ( "kernel32.dll" , ExactSpelling = true , SetLastError = true ) ]
160+ public static extern bool GenerateConsoleCtrlEvent ( int dwCtrlEvent , int dwProcessGroupId ) ;
161+
162+ [ DllImport ( "kernel32.dll" , ExactSpelling = true , SetLastError = true ) ]
163+ public static extern bool AttachConsole ( int dwProcessId ) ;
164+
165+ [ DllImport ( "kernel32.dll" , ExactSpelling = true , SetLastError = true ) ]
166+ public static extern bool FreeConsole ( ) ;
167+
168+ public delegate bool HANDLER_ROUTINE ( int dwCtrlType ) ;
169+
170+ [ DllImport ( "kernel32.dll" , ExactSpelling = true , SetLastError = true ) ]
171+ public static extern bool SetConsoleCtrlHandler ( HANDLER_ROUTINE HandlerRoutine , bool Add ) ;
172+
173+ // -------------------------------------------------------------------------------------------------------
174+
175+ public const int MAX_PATH = 260 ;
176+
177+ public const int ERROR_INSUFFICIENT_BUFFER = 0x0000007A ;
178+
179+ [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
180+ public static extern bool QueryFullProcessImageName ( IntPtr hProcess , uint dwFlags , StringBuilder lpExeName , ref uint lpdwSize ) ;
181+ }
182+ }
0 commit comments