Skip to content

Commit 6045050

Browse files
committed
Fixed errors and logic for "Environment" tab
1 parent 0534d1d commit 6045050

13 files changed

+1105
-718
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//*********************************************************************************************************************************
2+
//https://github.com/gapotchenko/Gapotchenko.FX/blob/master/Source/Gapotchenko.FX.Diagnostics.Process/Pal/IProcessMemoryAccessor.cs
3+
//*********************************************************************************************************************************
4+
5+
namespace WindowTextExtractor.Diagnostics
6+
{
7+
/// <summary>
8+
/// Provides low-level access to the process memory.
9+
/// </summary>
10+
interface IProcessMemoryAccessor
11+
{
12+
/// <summary>
13+
/// Gets the page size measured in bytes according to the granularity of memory access control.
14+
/// </summary>
15+
int PageSize { get; }
16+
17+
/// <summary>
18+
/// Reads the process memory.
19+
/// </summary>
20+
/// <param name="address">The address to start reading at.</param>
21+
/// <param name="buffer">The buffer to read to.</param>
22+
/// <param name="offset">The buffer offset to start reading to.</param>
23+
/// <param name="count">The count of bytes to read.</param>
24+
/// <param name="throwOnError">
25+
/// <para>
26+
/// Indicates whether to throw an exception on error.
27+
/// </para>
28+
/// <para>
29+
/// The support of this flag is optional; an adapter may just prefer to return -1 even when the flag is <c>true</c>.
30+
/// </para>
31+
/// </param>
32+
/// <returns>The count of read bytes or -1 on error.</returns>
33+
int ReadMemory(UniPtr address, byte[] buffer, int offset, int count, bool throwOnError);
34+
}
35+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//******************************************************************************************************************************
2+
//https://github.com/gapotchenko/Gapotchenko.FX/blob/master/Source/Gapotchenko.FX.Diagnostics.Process/Pal/ProcessBinaryReader.cs
3+
//******************************************************************************************************************************
4+
5+
using System.IO;
6+
using System.Text;
7+
8+
namespace WindowTextExtractor.Diagnostics
9+
{
10+
sealed class ProcessBinaryReader : BinaryReader
11+
{
12+
public ProcessBinaryReader(Stream input, Encoding encoding) :
13+
base(input, encoding)
14+
{
15+
}
16+
17+
public string ReadCString()
18+
{
19+
var sb = new StringBuilder();
20+
21+
for (; ; )
22+
{
23+
int c = Read();
24+
if (c == -1)
25+
throw new EndOfStreamException();
26+
27+
if (c == 0)
28+
{
29+
// End of string.
30+
break;
31+
}
32+
33+
sb.Append((char)c);
34+
}
35+
36+
return sb.ToString();
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)