Dennis Kriechel
Dennis Kriechel

Reputation: 3749

VBS RegRead doesn't return value

I've got the following code:

Dim objShell,failing_path,working_path

failing_path = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test"
working_path = "HKEY_LOCAL_MACHINE\SOFTWARE\7-zip\Path"

Set objShell = WScript.CreateObject("WScript.Shell")

WScript.Echo "Working: " & objShell.RegRead(working_path) 
WScript.Echo "Not Working: " & objShell.RegRead(failing_path)

When executing I will get the Path from the 7-zip Registrykey, but the Test Key returns following error:

enter image description here

Error says: Registry wasn't opened for reading.

Here the proof that the Test-Key exists:

enter image description here

What am I doing wrong? I also tried to read the key via oReg.GetStringValue but this always returned null.

Upvotes: 2

Views: 8866

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

When I tested this, I was able to read from non-Wow6432Node paths just fine, even when using a 32-bit cscript.exe from a 32-bit cmd.exe (Process Explorer showed image type 32-bit for both processes).

Perhaps it's something really simple? I was able to reproduce the behavior you described when I created a registry value with a spurious trailing space:

>>> key = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
>>> WScript.Echo sh.RegRead(key & "Test")
Unable to open registry key "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\
Uninstall\Test" for reading. (0x80070002)
>>> WScript.Echo sh.RegRead(key & "Test ")
asd

Upvotes: 0

Kul-Tigin
Kul-Tigin

Reputation: 16950

It seems like your script is running in a 32-Bit compatible scripting host but in a 64-bit OS. Since 32 bit applications are automatically redirected to the WOW6432Node areas of the registry in 64-Bit OSes, RegRead method tries to read 32-Bit equivalent path like

HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Test

instead

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Test

So, if the redirected path does not exist you receive that error as expected.

You may need to force run your scripts in 64-bit compatible scripting hosts to get rid of that kind of implicit registry redirects.

Upvotes: 2

Related Questions