From 309a5362c32b168f780f9ed5c27ad80f25565602 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Wed, 2 May 2018 11:58:57 +0200 Subject: [PATCH] fix(device): Report Device architecture or Null if it cannot reliably (#247) Report Device CPU architecture --- src/app/SharpRaven/Data/Context/Device.cs | 48 ++++++++++++++++++----- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/src/app/SharpRaven/Data/Context/Device.cs b/src/app/SharpRaven/Data/Context/Device.cs index 34211f27..42fec539 100644 --- a/src/app/SharpRaven/Data/Context/Device.cs +++ b/src/app/SharpRaven/Data/Context/Device.cs @@ -204,6 +204,7 @@ internal static Device Create() } } + /// /// Retrieves the architecture of this device. /// @@ -212,17 +213,46 @@ internal static string GetArchitecture() => #if HAS_RUNTIME_INFORMATION // x-plat: Known results: X86, X64, Arm, Arm64, RuntimeInformation.ProcessArchitecture.ToString(); -#else +#elif NET35 // Windows: Known results: AMD64, IA64, x86 Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine) - // Unix: Known results: i686, i386, x86_64, aarch64 (Target Machine is unsupported on Mono outside Windows) - ?? Environment.GetEnvironmentVariable("HOSTTYPE", EnvironmentVariableTarget.Process) - #if !NET35 - // https://github.com/mono/mono/blob/cdea795c0e4706abee0841174c35799690f63ccb/mcs/class/corlib/System.Runtime.InteropServices.RuntimeInformation/RuntimeInformation.cs - ?? (Environment.Is64BitProcess ? "X64" : "X86"); - #else - ?? (IntPtr.Size == 4 ? "X86" : "X64"); - #endif + ?? ProcessorArchitectureNet35(); +#else + // https://github.com/mono/mono/blob/cdea795c0e4706abee0841174c35799690f63ccb/mcs/class/corlib/System.Runtime.InteropServices.RuntimeInformation/RuntimeInformation.cs#L79 + Environment.Is64BitOperatingSystem ? "X64" : "X86"; +#endif + +#if NET35 + + // Will attempt to detect the architecture of the OS. Returns null if it cannot. + private static string ProcessorArchitectureNet35() + { + bool? is64Bit = null; + if (IntPtr.Size == 8) + { + is64Bit = true; + } + // Only XP SP2+ support this API + else if (Environment.OSVersion.Version.Major == 5 + && Environment.OSVersion.Version.Minor >= 1 + || Environment.OSVersion.Version.Major >= 6) + { + // 32 bit process on Windows could be WoW64, make P/Invoke call to verify + using (var p = Process.GetCurrentProcess()) + { + if (IsWow64Process(p.Handle, out var retVal)) + { + is64Bit = retVal; + } + } + } + + return !is64Bit.HasValue ? null : is64Bit.Value ? "X64" : "X86"; + } + + [DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool wow64Process); #endif } }