Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a registry key exists with WScript

Tags:

registry

wsh

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?

like image 507
kisin Avatar asked Jan 22 '23 17:01

kisin


1 Answers

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);
like image 135
D'Arcy Rittich Avatar answered Feb 01 '23 12:02

D'Arcy Rittich