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.
Best Answer
You could use following at top of your batch file:
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 respectivelycmd.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 withfor 32-bit Windows where environment variable ProgramFiles(x86) does not exist or with
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
.On Windows x64 it is additionally checked if there are files in
%SystemRoot%\Sysnative
. In this case the batch file is executed with 32-bitcmd.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-bitcmd.exe
, this is the directory containing the 64-bit console applications.Note:
%SystemRoot%\Sysnative
is not a directory. It is not possible tocd
to%SystemRoot%\Sysnative
or useif exist %SystemRoot%\Sysnative
.