How to detect we're running under the ARM64 version of Windows 10 in .NET?

风格不统一 提交于 2019-12-24 15:07:46

问题


I created a C# .NET console application that can run in Windows 10 x86, x64 and ARM64 (via emulator layer).

I would like to know how to detect that the application is running in those platforms. I know how to detect x86 and x64, but how to detect that the app is running inside ARM64?

This is a snapshot of Visual Studio running into my ARM64 System. You can see that it's detected as X86


回答1:


You will be able to detect the processor architecture by using

System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture

Which will then return an Architecure enum: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=netstandard-2.0




回答2:


OK, this code works:

public static class ArchitectureInfo
{
    public static bool IsArm64()
    {
        var handle = Process.GetCurrentProcess().Handle;
        IsWow64Process2(handle, out var processMachine, out var nativeMachine);

        return nativeMachine == 0xaa64;
    }

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool IsWow64Process2(
        IntPtr process, 
        out ushort processMachine, 
        out ushort nativeMachine
    );
}

(via Calling IsWowProcess2 from C# .NET (P/Invoke))



来源:https://stackoverflow.com/questions/54456140/how-to-detect-were-running-under-the-arm64-version-of-windows-10-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!