Search code examples
windowscmdregistry

Adding registry key using reg add creates strange entry - Wow6432Node


I am running Windows 7 Professional Service Pack 1 64 bit.

If I run the following command on a 32 bit program:

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath" /ve /d "C:\Python27" /f

What happens is that the registry gets updated like this:

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Wow6432Node\Python\PythonCore\2.7\InstallPath]

Why? And how can I fix this?

Here is the C code for the program which launches the command:

#include <Windows.h>
#include <stdio.h>

int main() {
        char* command = "c:\\temp\\setuppython.cmd";
        PROCESS_INFORMATION ProcessInformation;     
        STARTUPINFO Info;
        ZeroMemory( &Info, sizeof( Info ) );
        Info.cb = sizeof( Info );

        BOOL success = CreateProcess( NULL, command, NULL,
            NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
            NULL, NULL, &Info, &ProcessInformation);

        if(success) {
            // Wait for the process to complete
            WaitForSingleObject( ProcessInformation.hThread, INFINITE);
            printf("cmd %s has finished\n", command);
        }
        else {
            printf("CreateProcess failed for cmd %s\n", command);
        }
}

This program is run as a 32 bit executable. I cannot change this C source code unfortunately so I am looking to fix this in the cmd file somehow.

Here is setuppython.cmd:

@echo off

echo setting up python >>C:\temp\results.txt

if EXIST "C:\Program Files (x86)" (
    echo Running 64 bit Windows >>C:\temp\results.txt
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath" /ve /d "C:\Python27" /f
    echo added python reg path: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath" >>C:\temp\results.txt
    reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath" >>C:\CTS\HDC\results.txt
) else (
    echo Running 32 bit Windows >>C:\temp\results.txt
    reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath" /ve /d "C:\Python27" /f
    echo added python reg path: "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath" >>C:\temp\results.txt
    reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath" >>C:\temp\results.txt
)

echo finished setting up python >>C:\temp\results.txt

Output is like this:

on console:

The operation completed successfully.
ERROR: The system was unable to find the specified registry key or value.
cmd c:\temp\setuppython.cmd has finished.

And the contents of results.txt:

setting up python 
Running 64 bit Windows 
added python reg path: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath" 
finished setting up python 

Solution

  • On 64-bit Windows there are two different registry "views" for certain keys, one for 64-bit applications and one for 32-bit applications.

    32-bit specific data is stored under the Wow6432Node key but you should not use this key name in your code. When your batch file detects that it is running on 64-bit Windows it should add the /reg:32 switch to the reg.exe commands if your goal is to always write to the 32-bit part of the registry:

    if EXIST "C:\Program Files (x86)" (
        echo Running 64 bit Windows >>C:\temp\results.txt
        reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath" /ve /d "C:\Python27" /f /reg:32
        echo added python reg path: "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath" >>C:\temp\results.txt
        reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.7\InstallPath" /reg:32 >>C:\CTS\HDC\results.txt
    ) else (
        ...
    

    Note The Wow6432Node key is reserved. For compatibility, applications should not use this key directly.