Determine if the current version of Windows is 32-bit or 64-bit

Believe it or not, my installer is so old that it has no way to detect the 64-bit version of Windows.

Is there a call to the Windows DLL or (even better) an environment variable that will provide this information for Windows XP and Windows Vista?

One possible solution

I see that Wikipedia states that the 64-bit version of Windows XP and Windows Vista has a unique environment variable: %ProgramW6432% , so I assume that it will be empty on 32-bit Windows.

This variable points to the Program Files directory, which stores the entire installed Windows program and others. By default, English systems use C:\Program Files . On 64-bit versions of Windows (XP, 2003, Vista) there is also %ProgramFiles(x86)% , which is C:\Program Files (x86) by default and %ProgramW6432% , which is C:\Program Files by default. %ProgramFiles% depends on whether the process requesting the environment variable is 32-bit or 64-bit itself (this is caused by 64-bit Windows-on-Windows redirection).

+59
windows 64bit batch-file
Mar 02 '09 at 2:33
source share
22 answers

See the script package that is listed in How to check if a computer is running a 32-bit or 64-bit operating system . It also contains instructions for checking this from the registry:

You can use the following registry location to check if your computer is running on 32 or 64 bit Windows operating system:

 HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0 

In the right pane, you will see the following registry entries:

 Identifier REG_SZ x86 Family 6 Model 14 Stepping 12 Platform ID REG_DWORD 0x00000020(32) 

The aforementioned β€œx86” and β€œ0x00000020 (32)” indicate that the operating system version is 32 bits.

+9
Nov 27 '11 at 16:09
source share

To check for a 64-bit version of Windows on the command line, I use the following template:

test.bat:

 @echo off if defined ProgramFiles(x86) ( @echo yes @echo Some 64-bit work ) else ( @echo no @echo Some 32-bit work ) 

ProgramFiles(x86) is an environment variable automatically detected by cmd.exe (both 32-bit and 64-bit) on 64-bit Windows machines only.

+60
Dec 02 '09 at 13:53
source share

Here is what Delphi code checks to see if your program runs on a 64-bit operating system:

 function Is64BitOS: Boolean; {$IFNDEF WIN64} type TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall; var hKernel32 : Integer; IsWow64Process : TIsWow64Process; IsWow64 : BOOL; {$ENDIF} begin {$IFDEF WIN64} //We're a 64-bit application; obviously we're running on 64-bit Windows. Result := True; {$ELSE} // We can check if the operating system is 64-bit by checking whether // we are running under Wow64 (we are 32-bit code). We must check if this // function is implemented before we call it, because some older 32-bit // versions of kernel32.dll (eg. Windows 2000) don't know about it. // See "IsWow64Process", http://msdn.microsoft.com/en-us/library/ms684139.aspx Result := False; hKernel32 := LoadLibrary('kernel32.dll'); if hKernel32 = 0 then RaiseLastOSError; try @IsWow64Process := GetProcAddress(hkernel32, 'IsWow64Process'); if Assigned(IsWow64Process) then begin if (IsWow64Process(GetCurrentProcess, IsWow64)) then begin Result := IsWow64; end else RaiseLastOSError; end; finally FreeLibrary(hKernel32); end; {$ENDIf} end; 
+20
Oct 27 '09 at 10:25
source share

I tested the solution I suggested in my question:

Tested for Windows environment variable: ProgramW6432

If it is not empty, then it is 64-bit Windows.W

+13
Mar 17 '09 at 19:05
source share

From the script package:

 IF PROCESSOR_ARCHITECTURE == x86 AND PROCESSOR_ARCHITEW6432 NOT DEFINED THEN // OS is 32bit ELSE // OS is 64bit END IF 

Using the Windows API :

 if (GetSystemWow64Directory(Directory, MaxDirectory) > 0) // OS is 64bit else // OS is 32bit 

Sources:

+13
Nov 27 '11 at 16:20
source share

If you can make API calls, try using GetProcAddress / GetModuleHandle to check for IsWow64Process , which is only present on Windows with 64-bit versions.

You can also try the ProgramFiles (x86) environment variable used in Vista / 2008 for backward compatibility, but I'm not 100% sure about XP-64 or 2003-64.

Good luck

+8
Mar 02 '09 at 2:40
source share

I used this in a login script to detect 64-bit Windows

if "% ProgramW6432%" == "% ProgramFiles%" goto is64flag

+6
Mar 04 '10 at 23:29
source share

I don’t know which language you are using, but .NET has the PROCESSOR_ARCHITEW6432 environment variable if the OS is 64-bit.

If you want to find out if your application works with 32-bit or 64-bit, you can check IntPtr.Size . It will be 4 if it is running in 32-bit mode and 8 if it is running in 64-bit mode.

+4
Mar 02 '09 at 2:40
source share

For a single-line VBScript / WMI set that retrieves the bit number of the actual bits (32 or 64) of the OS or hardware, see http://csi-windows.com/toolkit/csi-getosbits

+4
Dec 14 2018-10-14
source share

I want to add what I use in shell scripts (but can be easily used in any language) here. The reason is that some of the solutions here do not work with WoW64, some use things that are not really intended for this (checking for the presence of the * (x86) folder) or do not work in cmd scripts. I believe this is the β€œright” way to do this and should be safe even in future versions of Windows.

  @echo off if /i %processor_architecture%==AMD64 GOTO AMD64 if /i %PROCESSOR_ARCHITEW6432%==AMD64 GOTO AMD64 rem only defined in WoW64 processes if /i %processor_architecture%==x86 GOTO x86 GOTO ERR :AMD64 rem do amd64 stuff GOTO EXEC :x86 rem do x86 stuff GOTO EXEC :EXEC rem do arch independent stuff GOTO END :ERR rem I feel there should always be a proper error-path! @echo Unsupported architecture! pause :END 
+3
Jul 08 2018-12-12T00:
source share

I do not know which version of Windows it exists on, but on Windows Vista and later this works:

 Function Is64Bit As Boolean Dim x64 As Boolean = System.Environment.Is64BitOperatingSystem If x64 Then Return true Else Return false End If End Function 
+2
Feb 02 2018-11-11T00:
source share

Many answers mention a call to IsWoW64Process() or related functions. This is not the right way. You should use GetNativeSystemInfo() , which was designed for this purpose. Here is an example:

 SYSTEM_INFO info; GetNativeSystemInfo(&info); if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) { // It a 64-bit OS } 

Also see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724340%28v=vs.85%29.aspx

+2
Mar 24 '15 at 4:42
source share

In C #:

 public bool Is64bit() { return Marshal.SizeOf(typeof(IntPtr)) == 8; } 

In VB.NET :

 Public Function Is64bit() As Boolean If Marshal.SizeOf(GetType(IntPtr)) = 8 Then Return True Return False End Function 
+1
Mar 02 '09 at 3:00
source share

I use this:

 @echo off if "%PROCESSOR_ARCHITECTURE%"=="AMD64" ( echo 64 BIT ) else ( echo 32 BIT ) 

It works on Windows XP, tested it on Windows XP Professional. Both are 64-bit and 32-bit.

+1
Jan 09 '15 at 13:03
source share

I tested the following batch file on Windows 7 x64 / x86 and Windows XP x86 and everything is fine, but I have not tried Windows XP x64 yet, but this will probably work:

 If Defined ProgramW6432 (Do x64 stuff or end if you are aiming for x86) else (Do x86 stuff or end if you are aiming for x64) 
0
Jan 2 2018-12-12T00:
source share

I know this is ancient, but here is what I use to detect Win764

 On Error Resume Next Set objWSHShell = CreateObject("WScript.Shell") strWinVer = objWSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\BuildLabEx") If len(strWinVer) > 0 Then arrWinVer = Split(strWinVer,".") strWinVer = arrWinVer(2) End If Select Case strWinVer Case "x86fre" strWinVer = "Win7" Case "amd64fre" strWinVer = "Win7 64-bit" Case Else objWSHShell.Popup("OS Not Recognized") WScript.Quit End Select 
0
Jan 19 2018-12-12T00:
source share

Using Windows Powershell, if the following expression returns true, then this is a 64-bit OS:

(([Array](Get-WmiObject -Class Win32_Processor | Select-Object AddressWidth))[0].AddressWidth -eq 64)

This has been accepted and modified using http://depsharee.blogspot.com/2011/06/how-do-detect-operating-system.html (method No. 3). I tested this on a 64-bit version of Win7 (both in 32 and 64 bit PowerShell sessions) and in XP 32 bit.

0
Jan 21 '14 at 10:15
source share

The best way is to simply check if there are two directories of program files: "Program Files" and "Program Files (x86). The advantage of this method is that you can do this when o / s is not running, for example, if the machine did not start and you want to reinstall the operating system

0
Apr 25 '14 at 7:42 on
source share

I wonder if I use

 get-wmiobject -class Win32_Environment -filter "Name='PROCESSOR_ARCHITECTURE'" 

I get AMD64 in both 32-bit and 64-bit ISE (on 64-bit Win7).

0
Jul 09 '14 at 3:17
source share

Another method created by eGerman that uses PE files of compiled executable files (does not rely on registry entries or environment variables):

 @echo off &setlocal call :getPETarget "%SystemRoot%\explorer.exe" if "%=ExitCode%" EQU "00008664" ( echo x64 ) else ( if "%=ExitCode%" EQU "0000014C" ( echo x32 ) else ( echo undefined ) ) goto :eof :getPETarget FilePath :: ~~~~~~~~~~~~~~~~~~~~~~ :: Errorlevel :: 0 Success :: 1 File Not Found :: 2 Wrong Magic Number :: 3 Out Of Scope :: 4 No PE File :: ~~~~~~~~~~~~~~~~~~~~~~ :: =ExitCode :: CPU identifier setlocal DisableDelayedExpansion set "File=%~1" set Cmp="%temp%\%random%.%random%.1KB" set Dmp="%temp%\%random%.%random%.dmp" REM write 1024 times 'A' into a temporary file if exist "%File%" ( >%Cmp% ( for /l %%i in (1 1 32) do <nul set /p "=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ) setlocal EnableDelayedExpansion ) else (endlocal &cmd /c exit 0 &exit /b 1) REM generate a HEX dump of the executable file (first 1024 Bytes) set "X=1" >!Dmp! ( for /f "skip=1 tokens=1,2 delims=: " %%i in ('fc /b "!File!" !Cmp!^|findstr /vbi "FC:"') do ( set /a "Y=0x%%i" for /l %%k in (!X! 1 !Y!) do echo 41 set /a "X=Y+2" echo %%j ) ) del !Cmp! REM read certain values out of the HEX dump set "err=" <!Dmp! ( set /p "A=" set /p "B=" REM magic number has to be "MZ" if "!A!!B!" neq "4D5A" (set "err=2") else ( REM skip next 58 bytes for /l %%i in (3 1 60) do set /p "=" REM bytes 61-64 contain the offset to the PE header in little endian order set /p "C=" set /p "D=" set /p "E=" set /p "F=" REM check if the beginning of the PE header is part of the HEX dump if 0x!F!!E!!D!!C! lss 1 (set "err=3") else ( if 0x!F!!E!!D!!C! gtr 1018 (set "err=3") else ( REM skip the offset to the PE header for /l %%i in (65 1 0x!F!!E!!D!!C!) do set /p "=" REM next 4 bytes have to contain the signature of the PE header set /p "G=" set /p "H=" set /p "I=" set /p "J=" REM next 2 bytes contain the CPU identifier in little endian order set /p "K=" set /p "L=" ) ) ) ) del !Dmp! if defined err (endlocal &endlocal &cmd /c exit 0 &exit /b %err%) REM was the signature ("PE\0\0") of the PE header found if "%G%%H%%I%%J%"=="50450000" ( REM calculate the decimal value of the CPU identifier set /a "CPUID=0x%L%%K%" ) else (endlocal &endlocal &cmd /c exit 0 &exit /b 4) endlocal &endlocal &cmd /c exit %CPUID% &exit /b 0 
0
Aug 20 '14 at 9:20
source share

Here is an easier way for batch scripts

  @echo off goto %PROCESSOR_ARCHITECTURE% :AMD64 echo AMD64 goto :EOF :x86 echo x86 goto :EOF 
0
Nov 20 '14 at
source share

Check the registry for HKLM \ SOFTWARE \ Wow6432Node - If it is there, the system is 64-bit - 32-bit, otherwise.

-2
Apr 04 '13 at 15:36
source share



All Articles