Angus Comber
Angus Comber

Reputation: 9708

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 

Upvotes: 0

Views: 2732

Answers (2)

CristiFati
CristiFati

Reputation: 41116

I've modified your .bat file so that no matter what, it should operate on "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath":

@echo off

set _RESULTS_FILE="C:\temp\results.txt"
set _PYTHON_REG_KEY="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath"

echo setting up python >>%_RESULTS_FILE%

if "%PROCESSOR_ARCHITECTURE:64=%" neq "%PROCESSOR_ARCHITECTURE%" (
    echo Running 64 bit Windows >>%_RESULTS_FILE%
    set _REG_VIEW_ARG=/reg:32
) else (
    echo Running 32 bit Windows >>%_RESULTS_FILE%
)

reg add %_PYTHON_REG_KEY% /ve /d "C:\Python27" /f %_REG_VIEW_ARG%
echo added python reg path: %_PYTHON_REG_KEY% >>%_RESULTS_FILE%
reg query %_PYTHON_REG_KEY% %_REG_VIEW_ARG%>>%_RESULTS_FILE%


echo finished setting up python >>%_RESULTS_FILE%

Notes:

  • I took the liberty of parameterizing the file (with respect of the results.txt file name and the registry key - so if one is to be modified to only do it in one place). As a side effect everything that went to "C:\CTS\HDC\results.txt" will now go to the default "C:\temp\results.txt" (but that could easily be changed)
  • I changed the 64bit test (%PROCESSOR_ARCHITECTURE% could be either x86 or AMD64; I am replacing "64" by the empty string "" and then compare the result with the original content). A user could manually create that folder on 32bit machines (could also modify the env var, but in this case, there's a great chance that something else will go wrong before reaching our file), so none of these checks is 100% reliable, but I prefer the one with the var. The difference is that for 32bit apps running on 64bit Win, it won't explicitly specify the /reg:32 flag (as the app will operate on the 32bit view anyway)
  • I didn't test it on 32bit Win

results.txt content (with set _PYTHON_REG_KEY="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\aaaa") after running it from 64 and 32 bit cmd windows:

setting up python 
Running 64 bit Windows 
added python reg path: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\aaaa" 

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\aaaa
    (Default)    REG_SZ    C:\Python27

finished setting up python 
setting up python 
Running 32 bit Windows 
added python reg path: "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\aaaa" 

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\aaaa
    (Default)    REG_SZ    C:\Python27

finished setting up python 

Upvotes: 3

Anders
Anders

Reputation: 101636

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.

Upvotes: 2

Related Questions