[英]Detect if the processor is 64-bit under 32 bit OS
通常,x86-64架構提供與x86的兼容性。 32位Windows(或其他操作系統)可以在x86-64處理器上運行。 (如果我錯了,請糾正我)。
我想知道是否有可能(在C ++中)32位Windows知道底層處理器是否為64位。 例如,如果Windows 7 32位在Core i5上運行,我們應該能夠知道處理器是64位(盡管Windows 7 32位正在運行)。
您可能會質疑這樣的要求:即使處理器是64位且OS是32位,64位進程也無法運行(如果我錯了,請糾正我)。 但程序的目的是了解處理器,而不是操作系統。 這個問題可能與此類似,但它沒有給出任何C ++程序的暗示。
這不是C ++解決方案,但它似乎適用於C#。
但是應該很容易轉換為C ++,因為關鍵點在於API結構SYSTEM_INFO和API GetNativeSystemInfo()
首先是對獲得信息的API的引用
[DllImport("kernel32.dll")]
public static extern void GetNativeSystemInfo
([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);
然后是結構SYSTEM_INFO和_PROCESSOR_INFO_UNION
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEM_INFO
{
internal _PROCESSOR_INFO_UNION uProcessorInfo;
public uint dwPageSize;
public IntPtr lpMinimumApplicationAddress;
public IntPtr lpMaximumApplicationAddress;
public IntPtr dwActiveProcessorMask;
public uint dwNumberOfProcessors;
public uint dwProcessorType;
public uint dwAllocationGranularity;
public ushort dwProcessorLevel;
public ushort dwProcessorRevision;
}
[StructLayout(LayoutKind.Explicit)]
public struct _PROCESSOR_INFO_UNION
{
[FieldOffset(0)]
internal uint dwOemId;
[FieldOffset(0)]
internal ushort wProcessorArchitecture;
[FieldOffset(2)]
internal ushort wReserved;
}
現在是一個簡化代碼和調用本機API的方法的枚舉
public enum ProcessorArchitecture
{
Unknown = 0,
Bit32 = 1,
Bit64 = 2,
Itanium64 = 3
}
static public ProcessorArchitecture ProcessorBits
{
get
{
ProcessorArchitecture pbits = ProcessorArchitecture.Unknown;
SYSTEM_INFO l_System_Info = new SYSTEM_INFO();
GetNativeSystemInfo(ref l_System_Info);
switch (l_System_Info.uProcessorInfo.wProcessorArchitecture)
{
case 9: // PROCESSOR_ARCHITECTURE_AMD64
pbits = ProcessorArchitecture.Bit64;
break;
case 6: // PROCESSOR_ARCHITECTURE_IA64
pbits = ProcessorArchitecture.Itanium64;
break;
case 0: // PROCESSOR_ARCHITECTURE_INTEL
pbits = ProcessorArchitecture.Bit32;
break;
default: // PROCESSOR_ARCHITECTURE_UNKNOWN
pbits = ProcessorArchitecture.Unknown;
break;
}
return pbits;
}
}
好吧,據我所知,你只能通過查看CPU信息本身來實現這一點。 我認為應該足夠(對於x86和amd64)來檢查CPU是否支持長模式 。
為此,您可以在x86上使用cpuid
指令。 通過你的帖子的Windows-ness,我猜你正在使用Microsoft C ++編譯器。 為此,有一個__cpuid
內在 。 可悲的是,Microsoft頁面上的描述以PBE
標志結束,而我的cpuinfo中的lm
標志稍后會出現三個標志。
查看AMD處理器的CPUID修改 ,您可以使用InfoType = 0x80000001
獲取LM
,結果位於最后返回的整數的第29位。 在英特爾處理器的CPUID修改中,相同的位指定EM64T
標志,該標志等效於AFAIK。
在這兩種情況下,您應首先使用InfoType = 0x80000000
來獲取最大有意義的InfoType
值。 如果它小於0x80000001
,那么你不應該進行上述檢查,而是假設不支持長模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.