views:

1489

answers:

3

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?

A: 

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

im trying to read my own register key.what permissions i need?
kisin
+1  A: 

You probably need to remove the trailing slash. If you use that, if 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);
RedFilter
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
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.
RedFilter
If you are actually trying to read a `value` (it appears on right hand side in `RegEdit`), then there should be no trailing slash.
RedFilter
you are right. is there any way i can check if key exists even if Default value not set?
kisin
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.
RedFilter
A: 

i am also getting the same type of error for reading the default printer in javascript:

I am using the below statement to read the registry for default printer.

var sDefault = "" var oShell = new ActiveXObject("wscript.Shell"); var sRegVal = 'HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device'

 sDefault = oShell.RegRead(sRegVal)

GetDefaultPrinter = sDefault What would regRead return in case it does not find the registry, I am not able to trap that, instead I am getting a javascript error like unable to read registry key ''HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device'

savvy