问题
I want to detect whether the current Windows OS is 32-bit or 64-bit. How to achieve it using C++? I don't want processor type I want OS's bit type. This is because you can install 32-bit OS on 64-bit processor.
回答1:
The function to call is IsWow64Process or IsWow64Process2. It tells your 32-bit application if it is running on a 64 bit Windows.
If the program is compiled for 64 bits, it will already know.
回答2:
If your code is 64-bit and running, then Windows is 64-bit - nothing to check. If your process is 32-bit call IsWow64Process() - 32-bit processes run in WOW64 on 64-bit Windows and without WOW64 otherwise.
回答3:
bool getWindowsBit(bool & isWindows64bit)
{
#if _WIN64
isWindows64bit = true;
return true;
#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");
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
}
回答4:
you can use IsWow64Process if your app is a 32 bit app, if it's true you are running on an x64 OS, else it's 32bit
回答5:
You need to use GetNativeSystemInfo. Given that you expect this to work on a 32-bit operating system, you need to use LoadLibrary + GetProcAddress so that you can deal with this function not being available. So if that fails, you know it is a 32-bit operating system. If not, SYSTEM_INFO.wProcessorArchitecture
gives you the real processor type instead of the emulated one.
回答6:
Use GetNativeSystemInfo function. It gets a LPSYSTEM_INFO
parameter to get what you want.
SYSTEM_INFO structure:
wProcessorArchitecture
The processor architecture of the installed operating system.
回答7:
Here is another way: GetSystemWow64Directory - "Retrieves the path of the system directory used by WOW64. This directory is not present on 32-bit Windows." and "On 32-bit Windows, the function always fails, and the extended error is set to ERROR_CALL_NOT_IMPLEMENTED
."
I personally am not sure about the usage of IsWow64Process
since in MSDN in the description of the IsWow64Process
there is the text "Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function."
回答8:
You can run the windows command systeminfo
as a process in your program.
#include <stdlib.h>
system("systeminfo");
One of the returning categories is System Type.
Its output reads: System Type: x86-based PC
, or System Type: x64-based PC
This may be a more complicated solution than the one provided by others but I thought I would add it as a possibility. (Maybe you are after additional info as well.)
回答9:
bool IsX64win()
{
UINT x64test = GetSystemWow64DirectoryA(NULL, 0);
if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) return FALSE;
else return TRUE;
}
回答10:
Answer for Newer Versions of Windows
While several people, and the Microsoft Docs, have suggested IsWow64Process2 for this, I have not seen any code examples in my research. That is why I wanted to contribute this answer to the community.
According to the running 32 bit applications documentation page, Microsoft recommends using the IsWow64Process2 for Windows 10 instead of IsWow64Process:
A 32-bit application can detect whether it is running under WOW64 by calling the IsWow64Process function (use IsWow64Process2 if targeting Windows 10).
This function works on Windows 10 version 1511 (client) and Windows Server 2016 and above.
This has two parameters via which information is returned: pProcessMachine and pNativeMachine. Both return Image File Machine Constants
pProcessMachine returns information about whether the target process is running under the WOW64 emulator or not, and if it is, what kind of process it is.
pNativeMachine returns information about the architecture of the Windows host.
Using both of these return values, one can determine if Windows is 32 bit or 64 bit (which is what the OP asked), and whether the process is running under WOW64 as well as whether the process is 32 or 64 bit.
Here is a function I wrote for these purposes:
BOOL getBits(BOOL& windowsIs32Bit, BOOL& isWOW64, BOOL& processIs32Bit)
{
USHORT ProcessMachine;
USHORT NativeMachine;
if (!IsWow64Process2(GetCurrentProcess(), &ProcessMachine, &NativeMachine)) {
std::cerr << "IsWOW64Process2 returned FALSE (failed). GetLastError returned: " << GetLastError() << std::endl;
return FALSE;
}
if (ProcessMachine == IMAGE_FILE_MACHINE_UNKNOWN) {
isWOW64 = FALSE;
if (NativeMachine == IMAGE_FILE_MACHINE_IA64 || NativeMachine == IMAGE_FILE_MACHINE_AMD64 || NativeMachine == IMAGE_FILE_MACHINE_ARM64) {
windowsIs32Bit = FALSE;
processIs32Bit = FALSE;
return TRUE;
}
if (NativeMachine == IMAGE_FILE_MACHINE_I386 || NativeMachine == IMAGE_FILE_MACHINE_ARM) {
windowsIs32Bit = TRUE;
processIs32Bit = TRUE;
return TRUE;
}
std::cerr << "Unknown Windows Architecture." << std::endl;
return FALSE;
}
windowsIs32Bit = FALSE;
isWOW64 = TRUE;
processIs32Bit = TRUE;
return TRUE;
}
Here is an example usage of the above function:
int main() {
BOOL windowsIs32Bit;
BOOL isWOW64;
BOOL processIs32Bit;
if (!getBits(windowsIs32Bit, isWOW64, processIs32Bit)) {
return -1;
}
std::cout << (windowsIs32Bit ? "Windows is 32 bit" : "Windows is 64 bit") << std::endl;
std::cout << (isWOW64 ? "This process *is* running under WOW64" : "This process is *not* running under WOW64") << std::endl;
std::cout << (processIs32Bit ? "This process is 32 bit" : "This process is 64 bit") << std::endl;
return 0;
}
I could only test two scenarios of the above code because I only have 64 bit Windows on a 64 bit machine. I do not have a 32 bit machine nor 32 bit Windows, nor do I have any ARM machines. If someone can test other scenarios, I would appreciate some feedback about whether the design I have done works for them.
I wrote an article that goes into greater depth explaining how the above code works.
回答11:
static bool is64bitOS()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
if((si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_IA64)||(si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_AMD64)==64)
{
return true;
}
else
{
return false;
}
}
回答12:
A simple check is if the EXE does not run, then it is a 64-bit executable running on a 32-bit machine. A 64-bit machine will always run a 32-bit executable.
From Microsoft,
Most programs designed for the 32-bit version of Windows will work on the 64-bit version of Windows. Notable exceptions are many antivirus programs.
Device drivers designed for the 32-bit version of Windows don't work on computers running a 64-bit version of Windows. If you're trying to install a printer or other device that only has 32-bit drivers available, it won't work correctly on a 64-bit version of Windows.
In Windows, however, you can also check for the existence of the Program Files (x86) folder as another simple check. No need to get fancy.
来源:https://stackoverflow.com/questions/7011071/detect-32-bit-or-64-bit-of-windows