Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a batch file in 64 bit mode from a batch file in 32 bit mode

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 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?

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.

like image 883
SkYWAGz Avatar asked Nov 23 '14 15:11

SkYWAGz


People also ask

How do I run a batch file in 64-bit?

Use "\Windows\Sysnative\cmd.exe /c" to run a batch file. This will create a 64-bit shell for executing the entire batch file.

How do I run a 32-bit command prompt on 64-bit?

One way to launch a 64-bit CMD is to just use "My Computer" and double click C:\Windows\System32\cmd.exe . One way to launch a 32-bit CMD is to do the same but double click C:\Windows\SysWOW64\cmd.exe .

How do I run a batch file in compatibility mode?

Just right click the batch file, open properties. In the compatibility tab, set the required compatibility. Note: AFAIK, Compatibility settings are set in the Windows Registry. Hence if you move the batch file to a different machine, the settings have to be re-applied.


1 Answers

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 the Microsoft documentation page File System Redirector.

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\cmd.exe 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.

like image 164
Mofi Avatar answered Sep 25 '22 11:09

Mofi