C++判断操作系统是32位还是64位

2024-11-21 17:53:00
推荐回答(2个)
回答1:

c++中检测系统是32位还是64位的方法如下:
1、定义一个函数getWindowsBit,传入布尔值:
bool getWindowsBit(bool & isWindows64bit)
{
预编译语句,判断32位成立,那么就把isWindows64bit置为true
#if _WIN64
isWindows64bit = true;
return true;
预编译语句,判断32位成立,那么就把isWow64置为false
#elif _WIN32
BOOL isWow64 = FALSE;
//IsWow64Process is not available on all supported versions of Windows.
//Use GetModuleHandle to get a handle to the DLL that contains the function
//and GetProcAddress to get a pointer to the function if available.

LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)
GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
2、实际的判断逻辑,根据处理器函数GetCurrentProcess来判断
if(fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(), &isWow64))
return false;
if(isWow64)
isWindows64bit = true;
else
isWindows64bit = false;
return true;
}
else
return false;
#else
assert(0);
return false;
#endif
}

回答2:

BOOL Is64Bit_OS()
{
BOOL bRetVal = FALSE;
SYSTEM_INFO si = { 0 };
LPFN_PGNSI pGNSI = (LPFN_PGNSI) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetNativeSystemInfo");
if (pGNSI == NULL)
return FALSE;
pGNSI(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )
bRetVal = TRUE;
else
//32 位操作系统
_tprintf(_T("is 32 bit OS\r\n"));
return bRetVal;
}