Fix 32-bit host context APIs and ExternalReader bitness detection
- Add GetThreadContext/SetThreadContext overloads accepting Context32 so a 32-bit process on a native 32-bit OS can capture x86 thread context. - DllInjector.InjectWithThreadHijack now selects the context API based on both process bitness and OS bitness: * 64-bit process -> native 64-bit context * 32-bit process on 64-bit OS -> WOW64 context * 32-bit process on 32-bit OS -> native x86 context - ExternalReader now validates that the caller supplied ProcessAccess.QueryInformation, and surfaces any IsWow64Process failure instead of silently falling back to host bitness. Tests: 207 passing, 4 skipped.
This commit is contained in:
@@ -37,6 +37,13 @@ public sealed class ExternalReader : MemoryBase
|
||||
public ExternalReader(System.Diagnostics.Process process, ProcessAccess desiredAccess = DefaultAccess)
|
||||
{
|
||||
_processId = process.Id;
|
||||
if ((desiredAccess & ProcessAccess.QueryInformation) == 0)
|
||||
{
|
||||
throw new ArgumentException(
|
||||
"ExternalReader requires ProcessAccess.QueryInformation to determine target bitness.",
|
||||
nameof(desiredAccess));
|
||||
}
|
||||
|
||||
_handle = NativeMethods.OpenProcess(desiredAccess, false, _processId);
|
||||
if (_handle.IsInvalid)
|
||||
{
|
||||
@@ -46,11 +53,12 @@ public sealed class ExternalReader : MemoryBase
|
||||
}
|
||||
|
||||
// Derive target bitness. A 64-bit host sees a 32-bit target as WOW64.
|
||||
// A 32-bit host can only open 32-bit targets. If the API fails, fall
|
||||
// back to the current process bitness (self-open path).
|
||||
// A 32-bit host can only open 32-bit targets.
|
||||
if (!NativeMethods.IsWow64Process(_handle, out bool wow64))
|
||||
{
|
||||
wow64 = false;
|
||||
int error = Marshal.GetLastPInvokeError();
|
||||
throw new InvalidOperationException(
|
||||
$"IsWow64Process failed for PID {_processId}: error {error}.");
|
||||
}
|
||||
|
||||
_is64Bit = Environment.Is64BitProcess && !wow64;
|
||||
|
||||
Reference in New Issue
Block a user