Solved

VBScript RegRead Function is not working as expected

Posted on 2011-02-17
13
1,414 Views
Last Modified: 2012-05-11
I have a simple function whereby I pass in the name of a Registry Key Path.
I have a const variable of HKLM = &H80000002.
I'm passing in the following Check on this path:
sKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog"

I just want to check to make sure that the "Eventlog" folder exists and that's it.
When I make the function call I've tried passing in the following different parameters:
If regKeyExists(sKeyPath) and If regKeyExists(HKLM & "\" & sKeyPath)

I'm getting this error and it's driving me nuts. I'm not sure what else it could be:
-2147024894

Please see code below.

Thanks Experts,
Wallace

Function regKeyExists(Key)
	On Error Resume Next
	
	Dim oShll, rKey
	Set oShll = CreateObject("WScript.Shell")
	rKey = oShll.RegRead(Key)
	MsgBox Err.Number
	If Err.Number <> 0 Then
		Err.Clear
		regKeyExists = False
	Else
		Err.Clear
		regKeyExists = True
	End If
	
	Set oShll = Nothing
End Function

Open in new window

0
Comment
Question by:wally_davis
  • 5
  • 5
  • 3
13 Comments
 
LVL 52

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 34920242
You need a trailing backslash, as in:

regKeyExists("HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\")

~bp
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34921284
Bill is right, but you can have your function make sure there is a trailing backslash as well.

Regards,

Rob.
Function regKeyExists(Key)
	On Error Resume Next
	
	If Right(Key, 1) <> "\" Then Key = Key & "\"
	Dim oShll, rKey
	Set oShll = CreateObject("WScript.Shell")
	rKey = oShll.RegRead(Key)
	MsgBox Err.Number
	If Err.Number <> 0 Then
		Err.Clear
		On Error GoTo 0
		regKeyExists = False
	Else
		Err.Clear
		On Error GoTo 0
		regKeyExists = True
	End If
	
	Set oShll = Nothing
End Function

Open in new window

0
 

Author Comment

by:wally_davis
ID: 34921954
Ok, I tried you're suggestions and to no avail. I'm doing some checking on the value that gets pass in to the Key variable (the parameter) in the Function regKeyExists(Key). I've tried prefixing the path sKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\" with the the string of "HKLM" and the actual const variable where HKLM = &H80000002. So, it could look like one of the two prior to passing it in to the Function:
1. If regKeyExists(HKLM & "\" & sKeyPath & "\") Then OR
2. If regKeyExists("HKLM\" & sKeyPath & "\") Then

In the function itself I've displayed the value in the "Key" variable parameter to equate to this:
rKey = oShll.RegRead(Key)
So, var Key contains the Value "-2147483646\SYSTEM\CurrentControlSet\Services\Event Archiver Service" as it gets passed into this Key variable. After that, the rKey = Empty and the error I get is still -2147024894.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34921997
Reg Read does not require any Hex constant values.  It only requires the standard user friendly string value of HKLM or HKCU, etc.

So, pass the value like this:

sKeyPath = "HKLM\SYSTEM\CurrentControlSet\Services\Event Archiver Service\"
If regKeyExists(sKeyPath) Then

Regards,

Rob.
sKeyPath = "HKLM\SYSTEM\CurrentControlSet\Services\Event Archiver Service\"

If regKeyExists(sKeyPath) Then
	MsgBox "Exists"
Else
	MsgBox "Does not exist"
End If

Function regKeyExists(Key)
	On Error Resume Next
	
	If Right(Key, 1) <> "\" Then Key = Key & "\"
	Dim oShll, rKey
	Set oShll = CreateObject("WScript.Shell")
	rKey = oShll.RegRead(Key)
	'MsgBox Err.Number
	If Err.Number <> 0 Then
		Err.Clear
		On Error GoTo 0
		regKeyExists = False
	Else
		Err.Clear
		On Error GoTo 0
		regKeyExists = True
	End If
	
	Set oShll = Nothing
End Function

Open in new window

0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 34922591
As I mentioned in my first post, you need:

regKeyExists("HKLM\SYSTEM\CurrentControlSet\Services\Eventlog\")

~bp
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34922669
LOL! Yes, that's true.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:wally_davis
ID: 34927981
One last note guys, bp and Rob, you've both been a tremendous help.

I ran into one other snag. Let's say I have a KeyPath whose value is equal to:
HKLM\SYSTEM\CurrentControlSet\Services\Wallaces Key\".

Note the space in the last key "Wallaces Key". When I pass this in, it gives me an error of
-2147024894. I think this might have something to do with the fact the Key has a space (or could have multiple spaces). I tried passing the Key in with double quotes like this and it still fails when passed to the Function --> regKeyPath("""" & "HKLM\" & sKeyPath & "\" & """").

I'm sure there's a way through this problem. I just haven't found the fix for it. Thanks guys!
0
 

Author Comment

by:wally_davis
ID: 34928438
While I was thinking (ya, I know, dangerous huh) I'm also wondering if the Error Number of
-2147024894 is just another way of saying "This Subkey does not exist and this is the error I'm giving you as a result."

If you look at the function below, I get on of two values returned to the variable "rKey".
I get either a double-quote ("") and a Error Number of 0 if the Key exists or the var "rKey" will get "Empty" returned to it and an Error Number of -2147024894. Your experiences or thoughts on how you would handle it?
Function regKeyExists(Key)
	On Error Resume Next
	
	Dim oShll, rKey
	Set oShll = CreateObject("WScript.Shell")
	rKey = oShll.RegRead(Key)
	
	MsgBox Err.Number
	
	If Err.Number <> 0 Then
		Err.Clear
		On Error Goto 0
		regKeyExists = False
	Else
		Err.Clear
		On Error Goto 0
		regKeyExists = True		
	End If
End Function

Open in new window

0
 

Author Comment

by:wally_davis
ID: 34928477
Ah crap, I'm getting ahead of myself. I completely forgot that the key "HKLM\SYSTEM\CurrentControlSet\Services\Wallaces Key\" does exist and I'm still getting the Error Number of -2147024894 returned to me so I'm still trying to figure out why.
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 250 total points
ID: 34931104
Spaces do work.  I can read
sKeyPath = "HKLM\SYSTEM\CurrentControlSet\Services\.NET CLR Data\"

with no problem.

It must be a permissions thing.  Check permissions on that key to make sure the account you're running the script with has access to it.

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34931108
If you're on Windows Vista or 7, try running the script as Administrator.  You can do this by making a batch file with
wscript "C:\Temp\RegReadTest.vbs"

and then right click the batch, and Run As Administrator.

Rob.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 34931160
Agreed, spaces are not a problem.

~bp
0
 

Author Closing Comment

by:wally_davis
ID: 35096269
Problem was two-fold. I needed a trailing backslash added to my code and it was also a permissions issue on the specific Server I was testing against. Thanks Bill and Rob!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Script is being strange 8 63
Need to Modify a Script I found 5 92
Protecting vb6 & .Net code Obfuscation 18 94
Can we place a tooltip on the actual vb6 form 5 35
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now