Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is64BitOperatingSystem
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01375729
Message ID:
01376985
Vues:
25
Hi Einar,

Not really and checking the IntPtr size is not reliable when compiling specifically for x86. I think a better way would be to check the size of page table entries as this is OS specific (but that gets complex).

The code below should help and is supports Window XP, Windows Server 2003 upwards:-
    public enum Platform
    {
      X86,
      X64,
      Unknown
    }

    internal const ushort PROCESSOR_ARCHITECTURE_INTEL = 0;
    internal const ushort PROCESSOR_ARCHITECTURE_IA64 = 6;
    internal const ushort PROCESSOR_ARCHITECTURE_AMD64 = 9;
    internal const ushort PROCESSOR_ARCHITECTURE_UNKNOWN = 0xFFFF;

    [StructLayout(LayoutKind.Sequential)]
    internal struct SYSTEM_INFO
    {
      public ushort wProcessorArchitecture;
      public ushort wReserved;
      public uint dwPageSize;
      public IntPtr lpMinimumApplicationAddress;
      public IntPtr lpMaximumApplicationAddress;
      public UIntPtr dwActiveProcessorMask;
      public uint dwNumberOfProcessors;
      public uint dwProcessorType;
      public uint dwAllocationGranularity;
      public ushort wProcessorLevel;
      public ushort wProcessorRevision;
    };

    [DllImport("kernel32.dll")]
    internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);

    [DllImport("kernel32.dll")]
    public static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);

    public static Platform GetPlatform()
    {
      SYSTEM_INFO sysInfo = new SYSTEM_INFO();

      if(System.Environment.OSVersion.Version.Major > 5 || (System.Environment.OSVersion.Version.Major == 5 &&
         System.Environment.OSVersion.Version.Minor >= 1))
      {
         GetNativeSystemInfo(ref sysInfo);
      }
      else
      {
         GetSystemInfo(ref sysInfo);
      }

      switch(sysInfo.wProcessorArchitecture)
      {
    case PROCESSOR_ARCHITECTURE_IA64:
    case PROCESSOR_ARCHITECTURE_AMD64:
      return Platform.X64;

    case PROCESSOR_ARCHITECTURE_INTEL:
      return Platform.X86;

    default:
      return Platform.Unknown;
      }
    }
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform