2009-09-10 11 views

Répondre

1

Vous pouvez utiliser PInvoke

Ceci est un exemple de code trouvé here.

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

private static Platform GetPlatform() 
{ 
    SYSTEM_INFO sysInfo = new SYSTEM_INFO(); 
    GetNativeSystemInfo(ref sysInfo); 

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

     case PROCESSOR_ARCHITECTURE_INTEL: 
      return Platform.X86; 

     default: 
      return Platform.Unknown; 
    } 
} 
+0

Ceci est une bonne solution si votre application .NET est en cours d'exécution avec un x86 forcé compilation drapeau pour d'autres raisons, mais vous devez encore déterminer si le système hôte est 64 bit. – Mike

2

La façon la plus simple est de le faire:

Int32 addressWidth = IntPtr.Size * 8; 

depuis IntPtr.Size est de 4 octets sur l'architecture 32 bits et 8 octets sur l'architecture 64 bits.

Questions connexes