检测当前 Windows 版本是 32 位还是 64 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect whether current Windows version is 32 bit or 64 bit
提问by Clay Nichols
Believe it or not, my installer is so old that it doesn't have an option to detect the 64-bit version of Windows.
信不信由你,我的安装程序太旧了,它没有检测 64 位 Windows 版本的选项。
Is there a Windows DLL call or (even better) an environment variable that would give that information for Windows XP and Windows Vista?
是否有 Windows DLL 调用或(甚至更好)可以为 Windows XP 和 Windows Vista 提供该信息的环境变量?
One possible solution
一种可能的解决方案
I see that Wikipedia states that the 64-bit version of Windows XP and Windows Vista have a unique environment variable: %ProgramW6432%
, so I'm guessing that'd be empty on 32-bit Windows.
我看到维基百科指出 64 位版本的 Windows XP 和 Windows Vista 有一个唯一的环境变量: %ProgramW6432%
,所以我猜这在 32 位 Windows 上是空的。
This variable points to Program Files
directory, which stores all the installed program of Windows and others. The default on English-language systems is C:\Program Files
. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)%
which defaults to C:\Program Files (x86)
and %ProgramW6432%
which defaults to C:\Program Files
. The %ProgramFiles%
itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).
该变量指向Program Files
目录,该目录存储了所有已安装的 Windows 程序和其他程序。英语语言系统上的默认值是C:\Program Files
. 在Windows(XP,2003,Vista)的64位版本,也有%ProgramFiles(x86)%
其默认为C:\Program Files (x86)
,并%ProgramW6432%
默认为C:\Program Files
。在%ProgramFiles%
本身取决于请求该环境变量中的进程是否为本身的32位或64位(这是由Windows的上-Windows 64位重定向引起的)。
采纳答案by Leif Gruenwoldt
See the batch script listed in How To Check If Computer Is Running A 32 Bit or 64 Bit Operating System. It also includes instructions for checking this from the Registry:
请参阅如何检查计算机是否运行 32 位或 64 位操作系统中列出的批处理脚本。它还包括从注册表中进行检查的说明:
You can use the following registry location to check if computer is running 32 or 64 bit of Windows operating system:
您可以使用以下注册表位置来检查计算机运行的是 32 位还是 64 位 Windows 操作系统:
HKLM\HARDWARE\DESCRIPTION\System\CentralProcessorIdentifier REG_SZ x86 Family 6 Model 14 Stepping 12
Platform ID REG_DWORD 0x00000020(32)
You will see the following registry entries in the right pane:
您将在右侧窗格中看到以下注册表项:
@echo off
if defined ProgramFiles(x86) (
@echo yes
@echo Some 64-bit work
) else (
@echo no
@echo Some 32-bit work
)
The above “x86” and “0x00000020(32)” indicate that the operating system version is 32 bit.
上面的“x86”和“0x00000020(32)”表示操作系统版本为32位。
回答by Dror Harari
To check for a 64-bit version of Windows in a command box, I use the following template:
要在命令框中检查 64 位版本的 Windows,我使用以下模板:
test.bat:
测试.bat:
function Is64BitOS: Boolean;
{$IFNDEF WIN64}
type
TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall;
var
hKernel32 : Integer;
IsWow64Process : TIsWow64Process;
IsWow64 : BOOL;
{$ENDIF}
begin
{$IFDEF WIN64}
//We're a 64-bit application; obviously we're running on 64-bit Windows.
Result := True;
{$ELSE}
// We can check if the operating system is 64-bit by checking whether
// we are running under Wow64 (we are 32-bit code). We must check if this
// function is implemented before we call it, because some older 32-bit
// versions of kernel32.dll (eg. Windows 2000) don't know about it.
// See "IsWow64Process", http://msdn.microsoft.com/en-us/library/ms684139.aspx
Result := False;
hKernel32 := LoadLibrary('kernel32.dll');
if hKernel32 = 0 then RaiseLastOSError;
try
@IsWow64Process := GetProcAddress(hkernel32, 'IsWow64Process');
if Assigned(IsWow64Process) then begin
if (IsWow64Process(GetCurrentProcess, IsWow64)) then begin
Result := IsWow64;
end
else RaiseLastOSError;
end;
finally
FreeLibrary(hKernel32);
end;
{$ENDIf}
end;
ProgramFiles(x86)
is an environment variable automatically defined by cmd.exe (both 32-bit and 64-bit versions) on Windows 64-bit machines only.
ProgramFiles(x86)
是仅在 Windows 64 位计算机上由 cmd.exe(32 位和 64 位版本)自动定义的环境变量。
回答by Blorgbeard is out
Here is some Delphi code to check whether your program is running on a 64 bit operating system:
以下是一些 Delphi 代码,用于检查您的程序是否在 64 位操作系统上运行:
IF PROCESSOR_ARCHITECTURE == x86 AND
PROCESSOR_ARCHITEW6432 NOT DEFINED THEN
// OS is 32bit
ELSE
// OS is 64bit
END IF
回答by Clay Nichols
I tested the solution I suggested in my question:
我测试了我在问题中建议的解决方案:
Tested for Windows Environment Variable: ProgramW6432
已针对 Windows 环境变量进行测试:ProgramW6432
If it's non empty then it's 64 bit Windows.W
如果它不是空的,那么它是 64 位 Windows.W
回答by Leif Gruenwoldt
From a batch script:
从批处理脚本:
if (GetSystemWow64Directory(Directory, MaxDirectory) > 0)
// OS is 64bit
else
// OS is 32bit
Using Windows API:
使用 Windows API:
@echo off
if /i %processor_architecture%==AMD64 GOTO AMD64
if /i %PROCESSOR_ARCHITEW6432%==AMD64 GOTO AMD64
rem only defined in WoW64 processes
if /i %processor_architecture%==x86 GOTO x86
GOTO ERR
:AMD64
rem do amd64 stuff
GOTO EXEC
:x86
rem do x86 stuff
GOTO EXEC
:EXEC
rem do arch independent stuff
GOTO END
:ERR
rem I feel there should always be a proper error-path!
@echo Unsupported architecture!
pause
:END
Sources:
资料来源:
回答by Jason
If you can make API calls, try using GetProcAddress/ GetModuleHandleto check for the existence of IsWow64Processwhich is only present in Windows OS that have 64-bit versions.
如果您可以进行 API 调用,请尝试使用GetProcAddress/ GetModuleHandle来检查IsWow64Process是否存在,它仅存在于具有 64 位版本的 Windows 操作系统中。
You could also try the ProgramFiles(x86)environment variable used in Vista/2008 for backwards compatibility, but I'm not 100% sure about XP-64 or 2003-64.
您也可以尝试使用 Vista/2008 中使用的ProgramFiles(x86)环境变量以实现向后兼容性,但我不是 100% 确定 XP-64 或 2003-64。
Good luck!
祝你好运!
回答by TallGuy
I used this within a login script to detect 64 bit Windows
我在登录脚本中使用它来检测 64 位 Windows
if "%ProgramW6432%" == "%ProgramFiles%" goto is64flag
如果 "%ProgramW6432%" == "%ProgramFiles%" 转到 is64flag
回答by Darwin
For a VBScript / WMI one-liner that retrieves the actuals bits number (32 or 64) of the OS or the Hardware, take a look at http://csi-windows.com/toolkit/csi-getosbits
对于检索操作系统或硬件的实际位数(32 或 64)的 VBScript/WMI 单行程序,请查看http://csi-windows.com/toolkit/csi-getosbits
回答by Andrew Ensley
I don't know what language you're using, but .NEThas the environment variable PROCESSOR_ARCHITEW6432
if the OS is 64-bit.
我不知道你使用的是什么语言,但如果操作系统是 64 位,.NET有环境变量PROCESSOR_ARCHITEW6432
。
If all you want to know is whether your application is running 32-bit or 64-bit, you can check IntPtr.Size
. It will be 4 if running in 32-bit mode and 8 if running in 64-bit mode.
如果您只想知道您的应用程序运行的是 32 位还是 64 位,您可以检查IntPtr.Size
. 如果在 32 位模式下运行,它将是 4,如果在 64 位模式下运行,它将是 8。
回答by Josef says Reinstate Monica
I want to add what I use in shell scripts (but can easily be used in any language) here. The reason is, that some of the solutions here don't work an WoW64, some use things not really meant for that (checking if there is a *(x86) folder) or don't work in cmd scripts. I feel, this is the "proper" way to do it, and should be safe even in future versions of Windows.
我想在这里添加我在 shell 脚本中使用的内容(但可以很容易地在任何语言中使用)。原因是,这里的一些解决方案在 WoW64 上不起作用,有些使用了并非真正意义上的东西(检查是否有 *(x86) 文件夹)或在 cmd 脚本中不起作用。我觉得,这是做到这一点的“正确”方式,即使在未来版本的 Windows 中也应该是安全的。
##代码##