4

im trying to check if a registry key exists and no matter what i try i always get the error message "unable to open registry key for reading"

the code im using:

keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\BOS\\BOSaNOVA TCP/IP\\Set 1\\Cfg\\Sign On\\";

try
{
    var shell = new ActiveXObject("WScript.Shell");
    var regValue = shell.RegRead(keyPath);

    shell = null;
}
catch(err)
{

}

what im missing here?

kisin
  • 177
  • 3
  • 4
  • 13

4 Answers4

7

You probably need to remove the trailing slash. If you use that, it will look for the default value for the key you have specified, and if it does not find it, will give that error.

Conversely, if you try to access a key as if it was a value by using no trailing slash, you will get the same error.

Some examples trying to access a key:

Fails:

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);

Succeeds (but gives empty result since Default value is empty):

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);

Some examples trying to access a value:

Succeeds (output is Value: C:\Program Files):

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);

Fails (shouldn't use trailing slash when accessing a value):

var keyPath = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir\\";
var shell = new ActiveXObject("WScript.Shell");
var regValue = shell.RegRead(keyPath);
WScript.Echo("Value: " + regValue);
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • im trying to read registry key. and as you wrote in your example, you can see that im using trailing slash with no luck. just to be sure i tried also without the slash and it obviously failed. what can i try next? – kisin Jan 18 '10 at 14:46
  • If you are try to read a `key` and not a `value` (difference explained here: http://en.wikipedia.org/wiki/Windows_Registry), then it is going to look for the `Default` value. If you do not have one, you will get an error. – D'Arcy Rittich Jan 18 '10 at 14:52
  • If you are actually trying to read a `value` (it appears on right hand side in `RegEdit`), then there should be no trailing slash. – D'Arcy Rittich Jan 18 '10 at 14:53
  • you are right. is there any way i can check if key exists even if Default value not set? – kisin Jan 18 '10 at 14:59
  • In Windows 7 which I am running, it seems that there is always a Default value. This may not be the case for earlier versions of Windows. You probably need to examine the error messages to know whether query failed due to missing Key or missing Default value. – D'Arcy Rittich Jan 18 '10 at 15:36
2

you are trying to open HKLM hive and probably WScript (or user you start it) have no permissions for that
you can look on permissions with regedt32

user204724
  • 160
  • 9
0

vbscript with single back slash and trailing slash in the end for the key works for me:

On Error Resume Next 
Set WSHShell = CreateObject("WScript.Shell")
s = WSHShell.RegRead( "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Word\" )

if Err.Number <> 0 then
    MsgBox(Err.Description)
    MsgBox("Office is not installed?" )
    exit Function
Else    
    MsgBox("Office is installed")
    exit Function
    ''wscript.quit
End If
MsgBox("xxxxxxxxxxxxxxxxx")
Sasha Bond
  • 984
  • 10
  • 13
0

What I got is ,

If the default value of the key is not been set then it will say unable to open registry key '---' for reading.

Now, if the key has a default value and you are not appending \\ after the key then also you will get the same error.

So, to get the default value you must add \\ , otherwise add the exact keyword listing under that key. e.g. "Version", "Location" etc.

Juan Sosa
  • 5,262
  • 1
  • 35
  • 41