How to Access the 32-bit Registry from a 64-bit Script
On a 64-bit version of Windows, the operating system maintains two separate views of the registry to support both 64-bit and 32-bit applications. This is part of the WoW64 (Windows on Windows 64-bit) compatibility layer. When a 32-bit program tries to access a key like HKEY_LOCAL_MACHINE\Software, Windows transparently redirects it to a special location: HKEY_LOCAL_MACHINE\Software\Wow6432Node.
By default, a standard 64-bit batch script will only see the 64-bit view of the registry. This creates a problem: how do you read or write a registry key for a 32-bit application from your 64-bit script? The solution is to use a special switch in the built-in REG.EXE command.
The Core Concept: Registry Redirection and the Wow6432Node
On a 64-bit OS:
- 64-bit applications read and write to
HKEY_LOCAL_MACHINE\Software. - 32-bit applications think they are reading and writing to
HKEY_LOCAL_MACHINE\Software, but Windows silently redirects them toHKEY_LOCAL_MACHINE\Software\Wow6432Node.
A standard 64-bit cmd.exe process will not see the 32-bit keys unless it specifically asks to.
The Core Command: REG.EXE /reg:32
The standard REG.EXE utility has a command-line switch that allows you to specify which "view" of the registry you want to access.
Syntax: REG <operation> <KeyName> ... /reg:32
/reg:32: This is the crucial switch. It tells theREGcommand to operate on the 32-bit view of the registry./reg:64: Can be used to explicitly specify the 64-bit view (this is the default).
This command requires administrator privileges if you are accessing the HKEY_LOCAL_MACHINE (HKLM) hive.
An Alternative Method: Calling the 32-bit reg.exe
A 64-bit Windows system has two versions of reg.exe. You can force a 32-bit operation by calling the 32-bit version directly from its location in the SysWOW64 folder.
Syntax: %windir%\SysWOW64\reg.exe <operation> <KeyName> ...
This method is less explicit than using the /reg:32 switch but achieves the same result. The /reg:32 switch is generally the recommended modern approach.
Basic Example: Querying a 32-bit Application's Key
Let's assume you have the 32-bit version of 7-Zip installed. Its uninstall information is stored in the 32-bit part of the registry.
@ECHO OFF
SET "RegKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip"
ECHO --- Accessing the Registry ---
ECHO.
ECHO Attempting to read with the default (64-bit) command...
REG QUERY "%RegKey%" /v DisplayName
ECHO.
ECHO Attempting to read with the /reg:32 switch...
REG QUERY "%RegKey%" /v DisplayName /reg:32
Output:
--- Accessing the Registry ---
Attempting to read with the default (64-bit) command...
ERROR: The system was unable to find the specified registry key or value.
Attempting to read with the /reg:32 switch...
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\7-Zip
DisplayName REG_SZ 7-Zip 19.00 (x86)
This clearly shows that the standard command fails, while the one with /reg:32 succeeds because it correctly looks in the Wow6432Node section.
How the /reg:32 Switch Works
When you use /reg:32, you are sending a flag with your request to the Windows Registry API. This flag tells the operating system's registry redirector to "treat this request as if it came from a 32-bit application." In response, the redirector automatically maps your request for HKLM\SOFTWARE\... to the correct HKLM\SOFTWARE\Wow6432Node\... location.
Common Pitfalls and How to Solve Them
Problem: The Script is Not Run as Administrator
If you are trying to query or modify the HKEY_LOCAL_MACHINE (HKLM) hive, you will need elevated privileges.
Solution: The script must be run as an Administrator.
Problem: This Only Works on a 64-bit OS
The /reg:32 switch and the SysWOW64 folder only exist on 64-bit versions of Windows. Running this command on a 32-bit OS will result in an error.
Solution: A robust script should first check if the OS is 64-bit.
IF NOT DEFINED ProgramFiles(x86) (
ECHO This is a 32-bit OS. The /reg:32 switch is not applicable.
GOTO :EOF
)
REM Proceed with the /reg:32 command...
CRITICAL: Do NOT include Wow6432Node in your path
This is the most common mistake. When you use the /reg:32 switch, you must provide the standard, non-redirected path.The operating system handles the redirection for you.
WRONG: REG QUERY "HKLM\Software\Wow6432Node\MyApp" /reg:32
CORRECT: REG QUERY "HKLM\Software\MyApp" /reg:32
Practical Example: Finding the Install Path of a 32-bit Application
This script needs to find where a 32-bit application is installed. It does so by querying the 32-bit "Uninstall" registry key for the application's InstallLocation.
@ECHO OFF
SETLOCAL
SET "InstallPath="
ECHO --- Finding the installation path for My 32-bit App ---
ECHO.
REM The standard path to the uninstall key.
SET "UninstallKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\My32bitApp"
REM Use FOR /F to parse the output of the REG command with the /reg:32 switch.
FOR /F "tokens=2*" %%A IN (
'REG QUERY "%UninstallKey%" /v InstallLocation /reg:32 2^>NUL'
) DO (
SET "InstallPath=%%B"
)
IF DEFINED InstallPath (
ECHO [SUCCESS] Application is installed at:
ECHO "%InstallPath%"
) ELSE (
ECHO [FAILURE] Could not find the application's installation path.
)
ENDLOCAL
Conclusion
The /reg:32 switch for the REG.EXE command is the essential tool for correctly interacting with the 32-bit registry on a 64-bit version of Windows.
Key takeaways:
- On a 64-bit OS, the registry is split into 64-bit and 32-bit (
Wow6432Node) views. - Use the
/reg:32switch to tell theREGcommand to access the 32-bit view. - When using this switch, provide the standard registry path and let Windows handle the redirection.
- You must run as an Administrator to access
HKEY_LOCAL_MACHINE. - This technique is critical for finding and managing settings for legacy 32-bit applications from a modern 64-bit script.