Batch to detect if system is a 32-bit or 64-bit

后端 未结 9 1410
心在旅途
心在旅途 2021-02-12 22:45

Does anybody know how to create a batch file that can shell one program if its a 64-bit system or shell another if its a 32-bit system?

9条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 22:59

    The below method should be pretty reliable since it will work even if environment variables have been messed with:

    rem If no kernel32.dll in System32, probably running on DOS or 16-bit Windows
    if not exist "%SystemRoot%\System32\kernel32.dll" goto DOS
    
    rem If no kernel32.dll in SysWOW64, likely a 32-bit Windows 
    if not exist "%SystemRoot%\SysWOW64\kernel32.dll" goto WIN32
    
    rem If file size reported for kernel32.dll located in System32 and SysWOW64 is
    rem the same, it likely means that System32 is being redirected to SysWOW64.
    rem This would be the case for 32-bit version of cmd.exe running on 64-bit OS. 
    for %%I in ("%SystemRoot%\SysWOW64\kernel32.dll") do (
      for %%J in ("%SystemRoot%\System32\kernel32.dll") do (
        if "%%~zI" equ "%%~zJ" goto WOW64
      )
    )
    
    rem If we get this far, the script is likely running in native 64-bit console
    echo Native shell on 64-bit Windows
    rem ...
    exit /b
    
    :WOW64
    echo 32-bit shell on 64-bit Windows (WOW64)
    rem ...
    exit /b
    
    :WIN32
    echo 32-bit Windows
    rem ...
    goto END
    
    :DOS
    echo DOS or 16-bit Windows
    rem ...
    goto END
    
    rem ...
    
    :END
    rem We can put this label at the end of the file to allow exiting script on 
    rem older systems that do not support 'exit /b'
    

    This method relies on the fact that "%WINDIR%\System32\kernel32.dll" should be present on all Windows systems. 64-bit versions of Windows also include "%WINDIR%\SysWOW64" directory containing 32-bit versions of system files, which is not present on 32-bit systems.

    On 64-bit systems, 32-bit applications are redirected to SysWOW64 when trying to access files in System32. So, if we get same size kernel32.dll from both System32 and SysWOW64, it means that redirection is in effect and our script is running in 32-bit console on 64-bit OS.

提交回复
热议问题