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

3

10

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.

Mariannmarianna answered 23/11, 2014 at 15:37 Comment(0)
U
12

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.

Undulation answered 23/11, 2014 at 19:20 Comment(0)
P
3
c:\windows\sysnative

Gives access to System32 for 32 bit programs.

32 Bit

C:\Windows\System32 accesses syswow64
c:\windows\sysnative accesses System32

64 Bit just does what's told, access the folders directly - eg C:\windows\system32 accesses System32 and C:\windows\syswow64 accesses Syswow64.

The point is you should only be writing a 32 bit program. 64 bit programs are mostly 32 bit internally (only memory addresses are 64 bit everything else remains 32 bits). 64 bits is for server apps. Use 32 bits for general programs.

EDIT

32 Bit programs are 32 bit with a 64 bit addressing mode of which 32 bits (base address is always 0 in Windows) are unused so only 32 bits (offset) is required for memory addresses.

64 Bit programs are 32 bit with a 64 bit offset memory address (I don't know the size of the base address in 64 bit mode as they are always 0 and have been irrelevent for decades). A 64 bit program can become a full 64 bit program, just by using 64 bit instructions when it chooses, generally for scientific or video processing tasks. But 64 bit everything chews too much memory and Windows' and other's libraries expect 32 bit values.

The general principal is that you need do nothing to achieve your tasks. People get into trouble when they start thinking about 32bit/64bit. If you ignore the bitness, Microsoft has put all the work in to make it just work.

If you type iexpress in Start - Run (Winkey + R) dialog you'll be able to make your own bat2exe.

Palindrome answered 23/11, 2014 at 19:13 Comment(4)
You may be interested in this social.msdn.microsoft.com/Forums/en-US/…Palindrome
I tried iexpress on my 64 bit OS and it worked, but I'm wondering if that same program could run in a 32 bit OS. I want my program to run in 64 bit mode becausesome registry stuff is not accessible with reg.exe called from a 32 bit batch file. I can't add a value to "HKLM\Software\Microsoft\Windows\CurrentVersion\Run" unless i directly run the batch file or I compile it in 64 bit mode (or with iExpress).Mariannmarianna
Is there an IExpress in SysWow64. Is there one in System32.Palindrome
I mean, will the iExpress program compiled on 64 bit OS work on a 32 bit OS ?Mariannmarianna
E
1

TL;DR version of Mofi's detailed answer:

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

rem Example: Run a command from the system folder:
%SystemPath%\dism

This sets %SystemPath% to whatever path which points to the native / "real" C:\Windows\System32 folder.

Rephrased explanation:

  • On a 32-bit OS, it will point to C:\Windows\System32, which is the only folder that exists.
  • On a 64-bit OS when running as a 32-bit process, it will point to the 64-bit C:\Windows\System32 via the path C:\Windows\Sysnative.
  • On a 64-bit OS when running as a 64-bit process, it will point to the 64-bit C:\Windows\System32 via the path C:\Windows\System32.

Reasoning:

  • On a 32-bit OS, there is only one 32-bit OS and no file system redirector.
  • On a 64-bit OS, when running as a 32-bit process, the path C:\Windows\System32 redirects to C:\Windows\SysWOW64, whereas C:\Windows\Sysnative redirects to C:\Windows\System32.
  • On a 64-bit OS, when running as a 64-bit process, the path C:\Windows\Sysnative does not exist, and C:\Windows\System32 points to C:\Windows\System32.

See official documentation.

Emmi answered 16/3, 2021 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.