4 avril 2025

Run on Tablet or not tablet ?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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 :

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