Wykrywanie, czy aktualna wersja systemu Windows jest 32-bitowa, czy 64-bitowa

Wierz lub nie, mój instalator jest tak stary, że nie ma opcji wykrywania 64-bitowej wersji systemu Windows.

Czy istnieje wywołanie DLL systemu Windows lub (jeszcze lepiej) zmienna środowiskowa, która dałaby te informacje dla Windows XP i Windows Vista?

Jedno możliwe rozwiązanie

Widzę, że Wikipedia stwierdza, że 64-bitowa wersja Windows XP i Windows Vista ma unikalną zmienną środowiskową: %ProgramW6432%, więc zgaduję, że na 32-bitowej będzie pusta Okna.

Ta zmienna wskazuje na katalog Program Files, który przechowuje wszystkie zainstalowane programy Windows i innych. Domyślną wartością w systemach anglojęzycznych jest C:\Program Files. W 64-bitowych wersjach systemu Windows (XP, 2003, Vista) istnieją również %ProgramFiles(x86)%, które domyślnie to C:\Program Files (x86) i %ProgramW6432%, które domyślnie to C:\Program Files. Sam %ProgramFiles% zależy od tego, czy proces żądający zmiennej środowiskowej jest 32-bitowy czy 64-bitowy (jest to spowodowane przekierowaniem Windows-on-Windows 64-bit).

Author: npocmaka, 2009-03-02

22 answers

Zobacz skrypt wsadowy wymieniony w Jak sprawdzić, czy na komputerze działa 32-bitowy lub 64-bitowy System operacyjny. Zawiera również instrukcje sprawdzania tego z rejestru:

Możesz użyć następującej lokalizacji rejestru, aby sprawdzić, czy na komputerze działa 32 lub 64-bitowy system operacyjny Windows:

HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0

W prawym okienku zobaczysz następujące wpisy rejestru:

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

Powyższe "x86 " i" 0x00000020(32) " wskazują, że system operacyjny wersja jest 32-bitowa.

 9
Author: Leif Gruenwoldt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-15 15:33:16

Aby sprawdzić 64-bitową wersję Windows w polu polecenia, używam następującego szablonu:

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) jest zmienną środowiskową zdefiniowaną automatycznie przez cmd.exe (zarówno w wersji 32-bitowej, jak i 64-bitowej) tylko na komputerach 64-bitowych Windows.

 62
Author: Dror Harari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 02:37:48

Oto kod Delphi, aby sprawdzić, czy twój program działa na 64-bitowym systemie operacyjnym:

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
Author: Blorgbeard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-18 18:49:37

Przetestowałem rozwiązanie, które zaproponowałem w moim pytaniu:

Testowana dla zmiennej środowiskowej Windows: ProgramW6432

Jeśli nie jest pusty, to jest to 64-bitowy Windows.W

 13
Author: Clay Nichols,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-18 13:25:12

Ze skryptu wsadowego:

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

Using Windows API:

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

Źródła:

  1. HOWTO: Detect bitness procesu
  2. funkcja GetSystemWow64Directory
 13
Author: Leif Gruenwoldt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 02:41:03

Jeśli możesz wykonywać połączenia API, spróbuj użyć GetProcAddress / GetModuleHandle aby sprawdzić istnienie IsWow64Process, który jest obecny tylko w systemie operacyjnym Windows, który ma wersje 64-bitowe.

Możesz również wypróbować zmienną środowiskowąProgramFiles(x86) używaną w Vista/2008 dla kompatybilności wstecznej, ale nie jestem w 100% pewien XP-64 lub 2003-64.

Powodzenia!

 8
Author: Jason,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-03-02 02:40:30

Użyłem tego w skrypcie logowania do wykrywania 64-bitowego systemu Windows

If "% ProgramW6432% " = = "% ProgramFiles % " goto is64flag

 6
Author: TallGuy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-04 23:29:40

Dla jednowierszowego VBScript / WMI, który pobiera liczbę bitów (32 lub 64) systemu operacyjnego lub sprzętu, spójrz na http://csi-windows.com/toolkit/csi-getosbits

 4
Author: Darwin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-12-14 14:39:27

Nie wiem, jakiego języka używasz, ale . NET ma zmienną środowiskową PROCESSOR_ARCHITEW6432 jeśli System Operacyjny jest 64-bitowy.

Jeśli chcesz tylko wiedzieć, czy Twoja aplikacja działa 32-bitowo, czy 64-bitowo, możesz sprawdzić IntPtr.Size. Będzie to 4, jeśli działa w trybie 32-bitowym i 8, jeśli działa w trybie 64-bitowym.

 4
Author: Andrew Ensley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 02:28:15

Chcę dodać to, czego używam w skryptach powłoki (ale może być łatwo używany w dowolnym języku) tutaj. Powodem jest to, że niektóre rozwiązania nie działają w WoW64, niektóre używają rzeczy nie do tego przeznaczonych(sprawdzanie, czy istnieje folder *(x86)) lub nie działają w skryptach cmd. Uważam, że jest to "właściwy" sposób na to i powinien być bezpieczny nawet w przyszłych wersjach systemu 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
Author: Josef,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-08 14:37:14

Nie wiem na jakiej wersji Windows istnieje, ale na Windows Vista i później to działa:

Function Is64Bit As Boolean
    Dim x64 As Boolean = System.Environment.Is64BitOperatingSystem
    If x64 Then
       Return true
    Else
       Return false
    End If
End Function
 2
Author: nicu96,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 02:39:22

Wiele odpowiedzi wspomina o wywołaniu IsWoW64Process() lub powiązanych funkcjach. To nie jest poprawny sposób. Należy użyć GetNativeSystemInfo(), który został zaprojektowany do tego celu. Oto przykład:

SYSTEM_INFO info;
GetNativeSystemInfo(&info);

if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
  // It's a 64-bit OS
}

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

 2
Author: jcoffland,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-24 04:42:49

W C#:

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

W VB.NET :

Public Function Is64bit() As Boolean
   If Marshal.SizeOf(GetType(IntPtr)) = 8 Then Return True
   Return False
End Function
 1
Author: Renaud Bompuis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-18 15:42:36

Używam tego:

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

Działa na Windows XP, przetestowano go na Windows XP Professional zarówno 64 bit, jak i 32 bit.

 1
Author: user83250,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-09 13:03:38

Wiem, że to starożytne, ale oto, czego używam do wykrywania 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
Author: Verbose,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-19 21:43:33

Przetestowałem następujący plik wsadowy na Windows 7 x64/x86 i Windows XP x86 i jest w porządku, ale jeszcze nie próbowałem Windows XP x64, ale to prawdopodobnie zadziała:

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
Author: Match,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-16 02:42:20

Używając Windows Powershell, Jeśli poniższe wyrażenie zwraca true, to jest to 64-bitowy system operacyjny:

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

To zostało pobrane i zmodyfikowane z: http://depsharee.blogspot.com/2011/06/how-do-detect-operating-system.html (Metoda #3). Testowałem to na Win7 64 bit (w obu 32 i 64 bitowych sesjach PowerShell) i XP 32 bit.

 0
Author: CJBS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-29 21:23:20

Najlepszym sposobem jest z pewnością sprawdzenie, czy istnieją dwa katalogi plików programów, ' Program Files 'i' Program Files (x86)' Zaletą tej metody jest to, że możesz to zrobić, gdy o / s nie jest uruchomiony, na przykład jeśli maszyna nie uruchomiła się i chcesz ponownie zainstalować system operacyjny

 0
Author: jasee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-25 07:42:47

Co ciekawe, jeśli użyję

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

Dostaję AMD64 zarówno w 32-bitowym jak i 64-bitowym ISE (na Win7 64-bit).

 0
Author: Mike Shepard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-09 03:17:28

Inny sposób stworzony przez eGerman , który używa numerów PE skompilowanych plików wykonywalnych (nie opiera się na rekordach rejestru ani zmiennych środowiskowych):

@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
Author: npocmaka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-20 09:20:58

Oto prostsza metoda skryptów wsadowych

    @echo off

    goto %PROCESSOR_ARCHITECTURE%

    :AMD64
    echo AMD64
    goto :EOF

    :x86 
    echo x86
    goto :EOF
 0
Author: Max,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-20 13:58:20

Sprawdź w rejestrze istnienie HKLM\SOFTWARE \ Wow6432Node - Jeśli tam jest, system jest 64-bitowy - 32-bit, inaczej.

 -2
Author: user1028775,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-04 15:36:05