Replace IsWow64Process2 P/Invoke with managed RuntimeInformation APIs#15608
Open
nohwnd wants to merge 2 commits intomicrosoft:mainfrom
Open
Replace IsWow64Process2 P/Invoke with managed RuntimeInformation APIs#15608nohwnd wants to merge 2 commits intomicrosoft:mainfrom
nohwnd wants to merge 2 commits intomicrosoft:mainfrom
Conversation
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>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Closes #15330