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

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

问题描述

我希望我的程序在32位操作系统中以32位模式运行,或者在64位OS中以64位模式运行。该程序是使用 Bat To Exe Converter v2.1.4 创建的,因此基本上是一个批处理文件。通常,当我在32位操作系统上运行批处理文件时,它以32位模式运行,而当我在64位操作系统上运行它时,它以64位模式运行,不是吗?问题是,使用 Bat To Exe Converter v2.1.4 ,我可以选择程序是32位还是64位。因此,我必须选择32,否则我认为它不能在32位操作系统上运行。我尝试使用.vbs文件使用 .Run .ShellExecute 重新启动程序,但结果是体系结构与转换器中的体系结构相同。我还尝试了 cmd / c %WINDIR%\System32\cmd.exe / c 以及%WINDIR%\SysWOW64\cmd.exe / c ,但我找不到解决方法。我使用Windows 8.0 x64,而我的VM是Windows 8.1 x64。

解决方案

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

  @echo off 
set SystemPath =%SystemRoot%\System32
如果不是%ProgramFiles (x86)% ==设置 SystemPath =%SystemRoot%\Sysnative

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



它如何工作?



环境变量 SystemPath 首先设置为Windows的System32目录。



打包到32位可执行文件中的批处理文件现在实际上从32位Windows上的System32目录运行所有控制台应用程序,但从%SystemRoot%\SysWOW64 目录(在64位Windows上)。



因此,批处理文件接下来检查环境变量存在ProgramFiles(x86),仅在Windows x64上是这种情况。因此,在Windows x86上,第三行的条件为false,并且 SystemPath 不变。但是在64位Windows上,将 SystemPath 修改为%SystemRoot%\Sysnative 以调用%SystemRoot%中的应用程序32System32 来自32位可执行文件,分别为 cmd.exe ,而没有重定向到%SystemRoot%\SysWOW64



有关更多详细信息,请参见文件系统重定向器文章。



但是最好这样做全部包含在32位可执行文件中,该可执行文件将批处理文件提取到%TEMP%并使用



<$ p $运行p> %SystemRoot%\System32\cmd.exe / C%TEMP%\ExtractedBatch.bat

用于32位Windows,其中环境变量 ProgramFiles(x86)不存在或与

一起使用$ p
$ b

 %SystemRoot%\Sysnative\cmd.exe / C%TEMP%\ExtractedBatch.bat 

在64位Windows上。



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

  @echo off 
set SystemPath =%SystemRoot%\System32
如果不是% ProgramFiles(x86)% ==(
,如果存在,则%SystemRoot%\Sysnative\ *设置为 SystemPath =%SystemRoot%\Sysnative

在Windows x64上,还会另外检查%SystemRoot%\Sysnative 。在这种情况下,批处理文件使用32位 cmd.exe 执行,并且仅在这种情况下%SystemRoot%\Sysnative 完全需要使用。否则%SystemRoot%\System32 也可以在Windows x64上使用,就像使用64位 cmd.exe 启动批处理文件时一样。 c $ c>,这是包含64位控制台应用程序的目录。



注意:%SystemRoot%\Sysnative 不是目录。 cd %SystemRoot%\Sysnative 或使用(如果存在)是不可能的SystemRoot%\Sysnative


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. That program is created with Bat To Exe Converter v2.1.4, so it's basicly 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? 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. 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. I also tried cmd /c and %WINDIR%\System32\cmd.exe /c and also %WINDIR%\SysWOW64\cmd.exe /c but I couldn't find a way to do it. 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"

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.

How it works?

The environment variable SystemPath is set first to System32 directory of 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.

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.

For more details see File System Redirector article in Microsoft Developer Network (MSDN).

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%\System32\cmd.exe /C "%TEMP%\ExtractedBatch.bat"

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

%SystemRoot%\Sysnative\cmd.exe /C "%TEMP%\ExtractedBatch.bat"

on 64-bit Windows.

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%\Sysnative\* set "SystemPath=%SystemRoot%\Sysnative"
)

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.

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天全站免登陆