[英]Windows command to tell whether a .dll file is 32 bit or 64 bit?
[英]Detect whether current Windows version is 32 bit or 64 bit
信不信由你,我的安装程序太旧了,它没有检测 64 位版本 Windows 的选项。
是否有 Windows DLL 调用或(甚至更好)环境变量可以为 Windows XP 和 Windows Vista 提供该信息?
一种可能的解决方案
我看到 Wikipedia 指出 64 位版本的 Windows XP 和 Windows Vista 有一个独特的环境变量: %ProgramW6432%
,所以我猜在 32 位 Windows 上它会是空的。
该变量指向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-on-Windows 64 位重定向引起的)。
要在命令框中检查 64 位版本的 Windows,我使用以下模板:
测试.bat:
@echo off
if defined ProgramFiles(x86) (
@echo yes
@echo Some 64-bit work
) else (
@echo no
@echo Some 32-bit work
)
ProgramFiles(x86)
是仅在 Windows 64 位计算机上由 cmd.exe(32 位和 64 位版本)自动定义的环境变量。
以下是一些 Delphi 代码,用于检查您的程序是否在 64 位操作系统上运行:
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;
我测试了我在问题中建议的解决方案:
已针对 Windows 环境变量进行测试:ProgramW6432
如果它不是空的,那么它是 64 位 Windows.W
从批处理脚本:
IF PROCESSOR_ARCHITECTURE == x86 AND
PROCESSOR_ARCHITEW6432 NOT DEFINED THEN
// OS is 32bit
ELSE
// OS is 64bit
END IF
使用 Windows API :
if (GetSystemWow64Directory(Directory, MaxDirectory) > 0)
// OS is 64bit
else
// OS is 32bit
资料来源:
请参阅如何检查计算机是否运行 32 位或 64 位操作系统中列出的批处理脚本。 它还包括从注册表中进行检查的说明:
您可以使用以下注册表位置来检查计算机运行的是 32 位还是 64 位 Windows 操作系统:
HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0
您将在右侧窗格中看到以下注册表项:
Identifier REG_SZ x86 Family 6 Model 14 Stepping 12
Platform ID REG_DWORD 0x00000020(32)
上面的“x86”和“0x00000020(32)”表示操作系统版本为32位。
如果您可以进行 API 调用,请尝试使用GetProcAddress / GetModuleHandle来检查IsWow64Process是否存在,它仅存在于具有 64 位版本的 Windows 操作系统中。
您也可以尝试使用 Vista/2008 中使用的ProgramFiles(x86)环境变量以实现向后兼容性,但我不是 100% 确定 XP-64 或 2003-64。
祝你好运!
我在登录脚本中使用它来检测 64 位 Windows
如果 "%ProgramW6432%" == "%ProgramFiles%" 转到 is64flag
对于检索操作系统或硬件的实际位数(32 或 64)的 VBScript/WMI 单行程序,请查看http://csi-windows.com/toolkit/csi-getosbits
我不知道您使用的是什么语言,但如果操作系统是 64 位, .NET具有环境变量PROCESSOR_ARCHITEW6432
。
如果您只想知道您的应用程序运行的是 32 位还是 64 位,您可以检查IntPtr.Size
。 如果在 32 位模式下运行,它将是 4,如果在 64 位模式下运行,它将是 8。
我想在这里添加我在 shell 脚本中使用的内容(但可以很容易地在任何语言中使用)。 原因是,这里的一些解决方案在 WoW64 上不起作用,有些使用了并非真正意义上的东西(检查是否有 *(x86) 文件夹)或在 cmd 脚本中不起作用。 我觉得,这是做到这一点的“正确”方式,即使在未来版本的 Windows 中也应该是安全的。
@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
很多答案都提到调用IsWoW64Process()
或相关函数。 这不是正确的方法。 您应该使用为此目的而设计的GetNativeSystemInfo()
。 下面是一个例子:
SYSTEM_INFO info;
GetNativeSystemInfo(&info);
if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
// It's a 64-bit OS
}
另请参阅: https : //msdn.microsoft.com/en-us/library/windows/desktop/ms724340%28v=vs.85%29.aspx
我不知道它存在于哪个 Windows 版本上,但在 Windows Vista 和更高版本上运行:
Function Is64Bit As Boolean
Dim x64 As Boolean = System.Environment.Is64BitOperatingSystem
If x64 Then
Return true
Else
Return false
End If
End Function
我用这个:
@echo off
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
echo 64 BIT
) else (
echo 32 BIT
)
它适用于 Windows XP,在 Windows XP Professional 64 位和 32 位上进行了测试。
在 C# 中:
public bool Is64bit() {
return Marshal.SizeOf(typeof(IntPtr)) == 8;
}
在VB.NET 中:
Public Function Is64bit() As Boolean
If Marshal.SizeOf(GetType(IntPtr)) = 8 Then Return True
Return False
End Function
使用 Windows Powershell,如果以下表达式返回 true,则它是 64 位操作系统:
(([Array](Get-WmiObject -Class Win32_Processor | Select-Object AddressWidth))[0].AddressWidth -eq 64)
这是从以下内容中获取和修改的: http : //depsharee.blogspot.com/2011/06/how-do-detect-operating-system.html (方法#3)。 我已经在 Win7 64 位(在 32 位和 64 位 PowerShell 会话中)和 XP 32 位上对此进行了测试。
最好的方法当然是检查是否有两个程序文件目录,'Program Files' 和 'Program Files (x86)' 这种方法的优点是你可以在 o/s 未运行时执行此操作,例如如果机器启动失败,您想重新安装操作系统
有趣的是,如果我使用
get-wmiobject -class Win32_Environment -filter "Name='PROCESSOR_ARCHITECTURE'"
我在 32 位和 64 位 ISE(在 Win7 64 位上)都获得了 AMD64。
eGerman 创建的另一种方法是使用编译后的可执行文件的 PE 编号(不依赖于注册表记录或环境变量):
@echo off &setlocal
call :getPETarget "%SystemRoot%\explorer.exe"
if "%=ExitCode%" EQU "00008664" (
echo x64
) else (
if "%=ExitCode%" EQU "0000014C" (
echo x32
) else (
echo undefined
)
)
goto :eof
:getPETarget FilePath
:: ~~~~~~~~~~~~~~~~~~~~~~
:: Errorlevel
:: 0 Success
:: 1 File Not Found
:: 2 Wrong Magic Number
:: 3 Out Of Scope
:: 4 No PE File
:: ~~~~~~~~~~~~~~~~~~~~~~
:: =ExitCode
:: CPU identifier
setlocal DisableDelayedExpansion
set "File=%~1"
set Cmp="%temp%\%random%.%random%.1KB"
set Dmp="%temp%\%random%.%random%.dmp"
REM write 1024 times 'A' into a temporary file
if exist "%File%" (
>%Cmp% (
for /l %%i in (1 1 32) do <nul set /p "=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
)
setlocal EnableDelayedExpansion
) else (endlocal &cmd /c exit 0 &exit /b 1)
REM generate a HEX dump of the executable file (first 1024 Bytes)
set "X=1"
>!Dmp! (
for /f "skip=1 tokens=1,2 delims=: " %%i in ('fc /b "!File!" !Cmp!^|findstr /vbi "FC:"') do (
set /a "Y=0x%%i"
for /l %%k in (!X! 1 !Y!) do echo 41
set /a "X=Y+2"
echo %%j
)
)
del !Cmp!
REM read certain values out of the HEX dump
set "err="
<!Dmp! (
set /p "A="
set /p "B="
REM magic number has to be "MZ"
if "!A!!B!" neq "4D5A" (set "err=2") else (
REM skip next 58 bytes
for /l %%i in (3 1 60) do set /p "="
REM bytes 61-64 contain the offset to the PE header in little endian order
set /p "C="
set /p "D="
set /p "E="
set /p "F="
REM check if the beginning of the PE header is part of the HEX dump
if 0x!F!!E!!D!!C! lss 1 (set "err=3") else (
if 0x!F!!E!!D!!C! gtr 1018 (set "err=3") else (
REM skip the offset to the PE header
for /l %%i in (65 1 0x!F!!E!!D!!C!) do set /p "="
REM next 4 bytes have to contain the signature of the PE header
set /p "G="
set /p "H="
set /p "I="
set /p "J="
REM next 2 bytes contain the CPU identifier in little endian order
set /p "K="
set /p "L="
)
)
)
)
del !Dmp!
if defined err (endlocal &endlocal &cmd /c exit 0 &exit /b %err%)
REM was the signature ("PE\0\0") of the PE header found
if "%G%%H%%I%%J%"=="50450000" (
REM calculate the decimal value of the CPU identifier
set /a "CPUID=0x%L%%K%"
) else (endlocal &endlocal &cmd /c exit 0 &exit /b 4)
endlocal &endlocal &cmd /c exit %CPUID% &exit /b 0
这是批处理脚本的一种更简单的方法
@echo off
goto %PROCESSOR_ARCHITECTURE%
:AMD64
echo AMD64
goto :EOF
:x86
echo x86
goto :EOF
较新版本的 Windows 的答案
今天,我在另一个问题上发布了一些代码,并解释了如何使用 IsWow64Process2 for Windows 10 version 1511 或更高版本和 Windows Server 2016 执行此操作。此外,代码确定进程是 32 位还是 64 位以及进程是否正在运行在WOW64模拟器下。
我发布答案的主要原因之一是,虽然有几个使用 IsWow64Process2 的建议,但我没有看到任何代码显示如何使用。
请在此处查看答案: https : //stackoverflow.com/a/59377888/1691559
您可以使用来自 npm 的名为 @wider/utils_where-am-i 的模块。 这可以在 Windows 机器和其他地方(如 linux)上的任何 javascript 环境中运行。 在 Windows 机器上,它提供一个具有 { os: 'win32' } 或 { os : 'win64' } 的对象。 它可以在 wshell、经典 ASP 或 nodeJS 中作为传统的纯 javascript 运行
我在 Windows 7 x64/x86 和 Windows XP x86 上测试了以下批处理文件,它很好,但我还没有尝试过 Windows XP x64,但这可能会起作用:
If Defined ProgramW6432 (Do x64 stuff or end if you are aiming for x86) else (Do x86 stuff or end if you are aiming for x64)
我知道这是古老的,但这是我用来检测 Win764 的
On Error Resume Next
Set objWSHShell = CreateObject("WScript.Shell")
strWinVer = objWSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\BuildLabEx")
If len(strWinVer) > 0 Then
arrWinVer = Split(strWinVer,".")
strWinVer = arrWinVer(2)
End If
Select Case strWinVer
Case "x86fre"
strWinVer = "Win7"
Case "amd64fre"
strWinVer = "Win7 64-bit"
Case Else
objWSHShell.Popup("OS Not Recognized")
WScript.Quit
End Select
检查注册表中是否存在 HKLM\\SOFTWARE\\Wow6432Node - 如果存在,则系统为 64 位 - 32 位,否则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.