advertise

Run on Tablet or not tablet ?

store

Ou plutôt ARM or not ARM

Voici une méthode que j’utilise pour déterminer si mon application Windows 8 fonctionne sur une tablette ou sur un PC. Je me base simplement sur le fait que le processeur de la tablette est un ARM ; J’en entends déjà crier « ah oui mais toutes les tablettes ne fonctionnent pas qu’avec des processeurs ARM, la Surface Pro est basée sur une architecture x86 … » C’est une méthode parmi d’autre ! 😉

Je regarde simplement dans ProcessorArchitecture de SYSTEM_INFO

 
using System;
using System.Runtime.InteropServices;
using Windows.System;

namespace Proc
{   
public class SysInfo
    {
        [DllImport("kernel32.dll")]
        private static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);

        //wProcessorArchitecture
        private const int PROCESSOR_ARCHITECTURE_INTEL = 0;
        private const int PROCESSOR_ARCHITECTURE_ARM = 5;
        private const int PROCESSOR_ARCHITECTURE_X64 = 9;
        private const int PROCESSOR_ARCHITECTURE_NEUTRAL = 11;
        private const int PROCESSOR_ARCHITECTURE_UNKNOWN = 65535;

        //http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=FR-FR&k=k(SYSTEM_INFO);k(TargetFrameworkMoniker-.NETCore,Version%3Dv4.5);k(DevLang-csharp)&rd=true
        [StructLayout(LayoutKind.Sequential)]
        private struct SYSTEM_INFO
        {
            public ushort wProcessorArchitecture;
            public ushort wReserved;
            public uint dwPageSize;
            public IntPtr lpMinimumApplicationAddress;
            public IntPtr lpMaximumApplicationAddress;
            public uint dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public ushort wProcessorLevel;
            public ushort wProcessorRevision;
        }

        public ProcessorArchitecture GetProcessorArchitecture()
        {
            SYSTEM_INFO si = new SYSTEM_INFO();
            GetNativeSystemInfo(ref si);
            switch (si.wProcessorArchitecture)
            {
                case PROCESSOR_ARCHITECTURE_INTEL:
                    return ProcessorArchitecture.X86;

                case PROCESSOR_ARCHITECTURE_ARM:
                    return ProcessorArchitecture.Arm;

                case PROCESSOR_ARCHITECTURE_X64:
                    return ProcessorArchitecture.X64;

                case PROCESSOR_ARCHITECTURE_UNKNOWN:
                    return ProcessorArchitecture.Unknown;

                default:
                    return ProcessorArchitecture.Neutral;
            }
        }
    }
}

Et ensuite on en fait ce qu’on en veut :

 
SysInfo SI = new SysInfo();
txtProc.Text=SI.GetProcessorArchitecture().ToString();

service
jobs
conditions
jobs