2009-09-10 8 views

risposta

1

È possibile utilizzare PInvoke

Questo è un esempio di codice trovato 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

Questa è una buona soluzione se il .NET applicazione è in esecuzione con un x86 forzata compilare bandiera per altri motivi, ma è ancora necessario per determinare se il sistema host è di 64 po. – Mike

2

Il modo più semplice è quello di fare questo:

Int32 addressWidth = IntPtr.Size * 8; 

dal IntPtr.Size è 4 byte su un'architettura a 32 bit e 8 byte su architettura a 64 bit.

Problemi correlati