【发布时间】:2011-02-14 10:03:01
【问题描述】:
有谁知道如何创建一个批处理文件,如果它是一个 64 位系统,它可以壳一个程序,如果它是一个 32 位系统,它可以壳另一个程序?
【问题讨论】:
标签: shell batch-file 32bit-64bit
有谁知道如何创建一个批处理文件,如果它是一个 64 位系统,它可以壳一个程序,如果它是一个 32 位系统,它可以壳另一个程序?
【问题讨论】:
标签: shell batch-file 32bit-64bit
检查%PROCESSOR_ARCHITECTURE% 是否为x86:
if %PROCESSOR_ARCHITECTURE%==x86 (
rem 32 bit
) else (
rem 64 bit
)
至少目前是这样。例如,在服务器上,我可以访问它的 AMD64,但不知道 Itanium 的外观。但是 32 位版本总是报告x86。
另一个选项,也适用于 WoW64:
for /f "skip=1 delims=" %%x in ('wmic cpu get addresswidth') do if not defined AddressWidth set AddressWidth=%%x
if %AddressWidth%==64 (
rem 64 bit
) else (
rem 32 bit
)
【讨论】:
cmd 实例在 64 位系统上工作。
cmd 实例可能已经由 32 位进程启动,并且用户可能只提供命令,而无法控制启动命令提示符的内容。 (哦,顺便说一句:如果 WMI 出于任何原因被禁用,则该解决方案将不起作用。)
它也可以在没有 WMI 的情况下工作! 我建议:
@echo off
if /i "%processor_architecture%"=="AMD64" GOTO AMD64
if /i "%PROCESSOR_ARCHITEW6432%"=="AMD64" GOTO AMD64
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
@echo Unsupported architecture "%processor_architecture%"!
pause
:END
【讨论】:
if /i "%processor_architecture%"=="x86" ( rem 32 ) else ( rem 64 )
uname -a #for mac
uname -i #for ubuntu
【讨论】:
arch。但我认为 OP 指的是 Windows。
简单的方法是测试%SystemRoot%\SysWOW64 文件夹是否存在。虽然它不是 100% 万无一失,但它非常适合检测系统是否为 64 位。
【讨论】:
那里的所有批次都不适用于我的 Windows 8 x64。
跟我一起工作:
@cd %programfiles(x86)%\
@if %ERRORLEVEL% == 0 (echo x64&&pause)
@if %ERRORLEVEL% == 1 (echo x86&&pause)
【讨论】:
这条线会给你你想要的,适用于 XP、Vista 和 7
将其保存为 .bat 或 .cmd
If Defined ProgramFiles(x86) (\\Fileserver\Distribution\Softwarex64.exe) else (\\Fileserver\Distribution\Softwarex86.exe)
如果本地机器中的安装源只是指向它(D:\Programs\Softwarex64.exe)
如果你只想运行命令而不是安装,只需在第一个 () 和 x86 的命令之间键入你想要的 x64 命令在第二个 () 之间
If Defined ProgramFiles(x86) (ipconfig /all & @echo This Is A 64-bit System ) else (arp -a & @echo This Is A 32-bit System)
复制到你的 CMD 中进行测试
希望对你有帮助
【讨论】:
在 Linux 上,您可以简单地在命令行中使用“arch”。
ubuntu# arch
x86_64
在 OSX (Snow Leopard) 上,它返回“i386”,即使您使用的是 64 位硬件。
【讨论】:
下面的方法应该是相当可靠的,因为即使环境变量被弄乱了它也能工作:
rem If no kernel32.dll in System32, probably running on DOS or 16-bit Windows
if not exist "%SystemRoot%\System32\kernel32.dll" goto DOS
rem If no kernel32.dll in SysWOW64, likely a 32-bit Windows
if not exist "%SystemRoot%\SysWOW64\kernel32.dll" goto WIN32
rem If file size reported for kernel32.dll located in System32 and SysWOW64 is
rem the same, it likely means that System32 is being redirected to SysWOW64.
rem This would be the case for 32-bit version of cmd.exe running on 64-bit OS.
for %%I in ("%SystemRoot%\SysWOW64\kernel32.dll") do (
for %%J in ("%SystemRoot%\System32\kernel32.dll") do (
if "%%~zI" equ "%%~zJ" goto WOW64
)
)
rem If we get this far, the script is likely running in native 64-bit console
echo Native shell on 64-bit Windows
rem ...
exit /b
:WOW64
echo 32-bit shell on 64-bit Windows (WOW64)
rem ...
exit /b
:WIN32
echo 32-bit Windows
rem ...
goto END
:DOS
echo DOS or 16-bit Windows
rem ...
goto END
rem ...
:END
rem We can put this label at the end of the file to allow exiting script on
rem older systems that do not support 'exit /b'
此方法依赖于“%WINDIR%\System32\kernel32.dll”应存在于所有 Windows 系统这一事实。 64 位版本的 Windows 还包括“%WINDIR%\SysWOW64”目录,其中包含 32 位版本的系统文件,这在 32 位系统上不存在。
在 64 位系统上,32 位应用程序在尝试访问 System32 中的文件时被重定向到 SysWOW64。因此,如果我们从 System32 和 SysWOW64 获得相同大小的 kernel32.dll,则意味着重定向生效,并且我们的脚本在 64 位操作系统的 32 位控制台中运行。
【讨论】: