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

2019-04-08 11:45发布

问题:

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.

回答1:

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



回答2:

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.