如何从 32 位模式的批处理文件以 64 位模式运行批处理文件 [英] How to run a batch file in 64 bit mode from a batch file in 32 bit mode

查看:37
本文介绍了如何从 32 位模式的批处理文件以 64 位模式运行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在 32 位操作系统中,我希望我的程序以 32 位模式运行,如果在 64 位操作系统中,我希望我的程序以 64 位模式运行.

I want my program to run in 32 bit mode if in a 32 bit OS or in 64 bit mode if it's in a 64 bit OS.

那个程序是用Bat To Exe Converter v2.1.4创建的,所以它基本上是一个批处理文件.通常,当我在 32 位操作系统上运行批处理文件时,它会以 32 位模式运行,而当我在 64 位操作系统上运行它时,它会以 64 位模式运行,不是吗?

That program is created with Bat To Exe Converter v2.1.4, so it's basically a batch file. Normally, when I run a batch file on a 32 bit OS it runs in 32 bit mode and when I run it on a 64 bit OS it runs in 64 bit mode, isn't it?

问题是,使用Bat To Exe Converter v2.1.4,我可以选择程序是32位还是64位.所以我必须选择 32 否则,我认为它不会在 32 位操作系统上运行.

The problem is, using Bat To Exe Converter v2.1.4, I can choose if the program is 32 or 64 bit. So I have to choose 32 or else, I don't think it will run on a 32 bit OS.

我尝试使用 .vbs 文件通过 .Run.ShellExecute 重新启动程序,但结果是架构与 中设置的架构相同转换器.

I tried using .vbs files to re-launch the program using .Run and .ShellExecute, but the result was the architecture being the same as the one set in the converter.

我也试过 cmd/c%WINDIR%System32cmd.exe/c 以及 %WINDIR%SysWOW64cmd.exe/c,但我找不到办法做到这一点.

I also tried cmd /c and %WINDIR%System32cmd.exe /c and also %WINDIR%SysWOW64cmd.exe /c, but I couldn't find a way to do it.

我使用 Windows 8.0 x64,我的 VM 是 Windows 8.1 x64.

I use Windows 8.0 x64 and my VM is Windows 8.1 x64.

推荐答案

您可以在批处理文件的顶部使用以下内容:

You could use following at top of your batch file:

@echo off
set "SystemPath=%SystemRoot%System32"
if not "%ProgramFiles(x86)%" == "" set "SystemPath=%SystemRoot%Sysnative"

接下来,您需要使用批处理文件中的 %SystemPath% 调用 Windows System32 目录中的每个控制台应用程序,例如 %SystemPath%findstr.exe.当然,您也可以使用 %SystemPath%cmd.exe 启动 cmd 以从批处理文件中始终运行 64 位命令行解释器.

Next you need to call every console application in System32 directory of Windows with %SystemPath% in your batch file, for example %SystemPath%findstr.exe. Of course you could also start cmd with %SystemPath%cmd.exe to run always 64-bit command line interpreter from within the batch file.

它是如何工作的?

环境变量SystemPath首先设置在Windows的System32目录下.

The environment variable SystemPath is set first to System32 directory of Windows.

打包成 32 位可执行文件的批处理文件现在运行所有控制台应用程序确实来自 32 位 Windows 上的 System32 目录,但来自 %SystemRoot%SysWOW64 目录在 64 位 Windows 上.

The batch file packed into a 32-bit executable runs now all console applications indeed from System32 directory on 32-bit Windows, but from %SystemRoot%SysWOW64 directory on 64-bit Windows.

因此,批处理文件接下来会检查环境变量 ProgramFiles(x86) 是否存在,这仅在 Windows x64 上存在.因此,Windows x86 上第三行的条件为 false,SystemPath 未更改.但是 SystemPath 在 64 位 Windows 上被修改为 %SystemRoot%Sysnative 以从 32 位调用 %SystemRoot%System32 中的应用程序分别可执行cmd.exe,无需重定向到%SystemRoot%SysWOW64.

Therefore the batch file checks next if environment variable ProgramFiles(x86) exists which is the case only on Windows x64. Therefore the condition on third line is false on Windows x86 and SystemPath is not changed. But SystemPath is modified to %SystemRoot%Sysnative on 64-bit Windows to call the applications in %SystemRoot%System32 from 32-bit executable respectively cmd.exe without redirection to %SystemRoot%SysWOW64.

有关详细信息,请参阅 Microsoft 文档页面 文件系统重定向器.

For more details see the Microsoft documentation page File System Redirector.

但最好在 32 位可执行文件中执行所有操作,该可执行文件将批处理文件提取到 %TEMP% 并使用

But better would be to do that all inside the 32-bit executable which extracts the batch file to %TEMP% and run it either with

%SystemRoot%System32cmd.exe /C "%TEMP%ExtractedBatch.bat"

对于环境变量 ProgramFiles(x86) 不存在或带有

for 32-bit Windows where environment variable ProgramFiles(x86) does not exist or with

%SystemRoot%Sysnativecmd.exe /C "%TEMP%ExtractedBatch.bat"

在 64 位 Windows 上.

on 64-bit Windows.

这里还有一个代码,它可以在批处理文件的顶部使用,以运行始终 64 位的控制台应用程序,独立于在 32 位或 64 位的 Windows x64 上启动 cmd.exe.

Here is one more code which can be used at top of a batch file to run always 64-bit console applications independent on being started on Windows x64 with 32-bit or with 64-bit cmd.exe.

@echo off
set "SystemPath=%SystemRoot%System32"
if not "%ProgramFiles(x86)%" == "" if exist %SystemRoot%Sysnativecmd.exe set "SystemPath=%SystemRoot%Sysnative"

在 Windows x64 上,它会另外检查 %SystemRoot%Sysnative 中是否有文件.在这种情况下,批处理文件使用 32 位 cmd.exe 执行,并且只有在这种情况下才需要使用 %SystemRoot%Sysnative.否则 %SystemRoot%System32 也可以在 Windows x64 上使用,因为当批处理文件以 64 位 cmd.exe 启动时,这是包含 64-位控制台应用程序.

On Windows x64 it is additionally checked if there are files in %SystemRoot%Sysnative. In this case the batch file is executed with 32-bit cmd.exe and only in this case %SystemRoot%Sysnative needs to be used at all. Otherwise %SystemRoot%System32 can be used also on Windows x64 as when the batch file is started with 64-bit cmd.exe, this is the directory containing the 64-bit console applications.

注意:%SystemRoot%Sysnative 不是目录.不可能将 cd 转换为 %SystemRoot%Sysnative 或使用 如果存在 %SystemRoot%Sysnative.

Note: %SystemRoot%Sysnative is not a directory. It is not possible to cd to %SystemRoot%Sysnative or use if exist %SystemRoot%Sysnative.

这篇关于如何从 32 位模式的批处理文件以 64 位模式运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆