10

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

enter image description here

SuperJMN
  • 13,110
  • 16
  • 86
  • 185

2 Answers2

4

You will be able to detect the processor architecture by using

System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture

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

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • Plenty of namespaces under _System.Runtime.InteropServices_ can be accessable via link that is shared by @JamieRees . You can find some other informations about your development area. – Tunahan Jan 31 '19 at 10:31
  • 1
    I finally have been able to try your solution, and it's confusing because System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture returns X86 even running INSIDE the ARM64 machine. – SuperJMN Feb 05 '19 at 12:59
  • Please, take a look at the screenshot I added to my first post. – SuperJMN Feb 05 '19 at 13:03
  • @SuperJMN: Is that possible that the code ran with a x86 dotnet runtime, on the windows emulation layer? I guess that if there is an enum that says "Arm64", it must be used somewhere... – cube45 Feb 08 '21 at 18:17
  • This is the correct answer now. Mind changing it @SuperJMN? – Sedat Kapanoglu Nov 15 '22 at 05:00
0

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))

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • 2
    You accepted this answer, but what about the answer to the other question, whose code you used here? Also, the code here is wrong because it does not check for errors when calling `IsWow64Process2`. – David Heffernan Feb 06 '19 at 11:19