Skip to content

Replace IsWow64Process2 P/Invoke with managed RuntimeInformation APIs#15608

Draft
nohwnd wants to merge 2 commits intomicrosoft:mainfrom
nohwnd:replace-wow64
Draft

Replace IsWow64Process2 P/Invoke with managed RuntimeInformation APIs#15608
nohwnd wants to merge 2 commits intomicrosoft:mainfrom
nohwnd:replace-wow64

Conversation

@nohwnd
Copy link
Copy Markdown
Member

@nohwnd nohwnd commented Mar 27, 2026

Summary

Replaces native IsWow64Process2 P/Invoke calls with managed System.Runtime.InteropServices.RuntimeInformation APIs, reducing dependency on Windows-version-specific native APIs.

Changes

  • PlatformEnvironment.Architecture (net462/netstandard2.0): Replaced \IsWow64Process2\ + \IMAGE_FILE_MACHINE_ARM64\ check with \RuntimeInformation.OSArchitecture\ switch. Eliminates P/Invoke entirely and the try/catch fallback for older Windows versions.

  • ProcessHelper.GetCurrentProcessArchitecture (net462/netstandard2.0): Replaced \IntPtr.Size\ + \IsWow64Process2\ logic with \RuntimeInformation.ProcessArchitecture\ switch, matching the existing .NET Core implementation.

  • ProcessHelper.GetProcessArchitecture (all targets): For the current process, delegates to \GetCurrentProcessArchitecture(). For remote processes, uses the simpler \IsWow64Process\ API (available since Windows XP SP2) combined with \RuntimeInformation.OSArchitecture\ to distinguish ARM64 native from x64 emulated via PE header check.

  • NativeMethods: Removed \IsWow64Process2\ declaration and \IMAGE_FILE_MACHINE_UNKNOWN\ constant. Kept \IsWow64Process\ (for remote process WOW64 detection) and \IMAGE_FILE_MACHINE_ARM64\ (for PE header check).

  • Added \System.Runtime.InteropServices.RuntimeInformation\ NuGet package reference for the net462 target.

Semantic preservation

\IsWow64Process2\ returns both \processMachine\ and
ativeMachine. The replacement preserves the exact semantics:

  • WOW64 process → X86 (same as \processMachine != IMAGE_FILE_MACHINE_UNKNOWN)
  • Non-WOW64 on ARM64 OS + ARM64 PE → ARM64
  • Non-WOW64 on ARM64 OS + non-ARM64 PE → X64 (x64 emulated)
  • Non-WOW64 on non-ARM64 OS → X64

Closes #15330

Replace IsWow64Process2 native calls with managed RuntimeInformation APIs:

- PlatformEnvironment.Architecture: Use RuntimeInformation.OSArchitecture
  instead of IsWow64Process2 to detect OS architecture. Eliminates P/Invoke
  and the try/catch fallback for older Windows versions.

- ProcessHelper.GetCurrentProcessArchitecture: Use
  RuntimeInformation.ProcessArchitecture with a switch expression, matching
  the existing .NET Core implementation.

- ProcessHelper.GetProcessArchitecture: For the current process, delegate to
  GetCurrentProcessArchitecture(). For remote processes, use the simpler
  IsWow64Process API combined with RuntimeInformation.OSArchitecture to
  distinguish ARM64 native from x64 emulated (via PE header check).

- Remove IsWow64Process2 and IMAGE_FILE_MACHINE_UNKNOWN from NativeMethods.
  Keep IsWow64Process for remote process WOW64 detection, and keep
  IMAGE_FILE_MACHINE_ARM64 for the PE header check.

- Add System.Runtime.InteropServices.RuntimeInformation NuGet package for
  net462 target framework.

Closes microsoft#15330

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 27, 2026 16:59
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd
Copy link
Copy Markdown
Member Author

nohwnd commented Apr 10, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

<Reference Include="System.Configuration" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to it's own item group.

@nohwnd nohwnd marked this pull request as draft April 10, 2026 14:27
@nohwnd
Copy link
Copy Markdown
Member Author

nohwnd commented Apr 10, 2026

not sure, need more time to review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

review usage of IsWow64Process2 and see if it is possible to replace them with GetSystemInfo and GetNativeSystem info

1 participant