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

I want my program to work in 32-bit mode if it is in a 32-bit OS or in 64-bit mode if it is in a 64-bit OS. This program was created using Bat To Exe Converter v2.1.4, so it is basically a batch file. Usually, when I run a batch file on a 32-bit OS, it works in 32-bit mode, and when I run it on a 64-bit OS, it works in 64 bit mode, right? The problem is that with Bat To Exe Converter v2.1.4 I can choose whether the program will be 32 or 64 bit. Therefore, I need to choose 32, otherwise I do not think that it will work on a 32-bit OS. I tried using .vbs files to restart the program using .Run and .ShellExecute, but the result was an architecture that was the same as in the converter. I also tried cmd /c and %WINDIR%\System32\cmd.exe /c , as well as %WINDIR%\SysWOW64\cmd.exe /c , but I could not find a way to do this. I am using Windows 8.0 x64, and my VM is Windows 8.1 x64.

+5
source share
2 answers

You can use the following at the top of your batch file:

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

Then you need to call any console application in the System32 Windows directory with %SystemPath% in the batch file, for example %SystemPath%\findstr.exe . Of course, you can also run cmd with %SystemPath%\cmd.exe to always launch the 64-bit command line interpreter from a batch file.

How it works?

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

A batch file packaged in a 32-bit executable now starts all console applications from the System32 directory on 32-bit Windows, but from the %SystemRoot%\SysWOW64 on 64-bit Windows.

Therefore, the batch file checks the following if the ProgramFiles (x86) environment variable exists, which occurs only for Windows x64. Therefore, the condition on the third line is incorrect for Windows x86, and SystemPath has not been changed. But SystemPath changed to %SystemRoot%\Sysnative on 64-bit Windows to call applications from %SystemRoot%\System32 from the 32-bit executable file respectively cmd.exe without redirecting to %SystemRoot%\SysWOW64 .

For more information, see the File Redirector on the Microsoft Developer Network (MSDN).

But it would be better to do all this inside a 32-bit executable file, which extracts the batch file in %TEMP% and launches it either with

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

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

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

on 64-bit Windows.

Here is another code that you can use at the top of the batch file to always launch 64-bit console applications, whether it runs on Windows x64 with 32-bit or 64-bit cmd.exe .

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

Windows x64 additionally checks for the presence of files in %SystemRoot%\Sysnative . In this case, the batch file is executed with 32-bit cmd.exe and only in this case %SystemRoot%\Sysnative should be used at all. Otherwise, %SystemRoot%\System32 can also be used on Windows x64, as when running a batch file with 64-bit cmd.exe , it is a directory containing 64-bit console applications.

Note: %SystemRoot%\Sysnative not a directory. Cannot cd to %SystemRoot%\Sysnative or use if exist %SystemRoot%\Sysnative

+5
source
 c:\windows\sysnative 

Provides access to System32 for 32-bit programs.

32 bit

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

64 Bit just does what is said, directly access folders - for example, C: \ windows \ system32 accesses System32 and C: \ windows \ syswow64 accesses Syswow64.

The fact is that you should write only a 32-bit program. 64-bit programs are mostly 32-bit internally (only 64-bit memory addresses; everything else remains 32-bit). 64 bit for server applications. Use 32 bits for general programs.

EDIT

32-bit programs are 32-bit with 64-bit addressing, of which 32 bits (the base address is always 0 in Windows) are not used, so only 32 bits are required for memory addresses (offset).

64-bit programs are 32-bit with a 64-bit offset memory address (I do not know the size of the base address in 64-bit mode, since they have always been 0 and have been out of place for decades). A 64-bit program can become a complete 64-bit program simply by using 64-bit instructions when it selects, as a rule, for scientific or video processing tasks. But 64-bit still chews too much memory, and Windows and other libraries expect 32-bit values.

The general principle is that you do not need to do anything to achieve your goals. People start to worry when they start thinking about 32bit / 64bit. If you ignore bitta, Microsoft has done all its work to make it work.

If you type iexpress in the Start-Run dialog box (Winkey + R), you can create your own bat2exe.

+3
source

Source: https://habr.com/ru/post/1207533/


All Articles