|
-
Mar 22nd, 2008, 09:35 AM
#1
Thread Starter
Fanatic Member
How to check if registry key exists
Im working with Wscript.Shell object.
Is there any way to check if a registry key exists.
If i want to read a value from a registry key, and if it doesnt exists i get an error.
I want to check first if a value exists and if it does then i want to read from it.
im reading registry keys with those commands:
Code:
set wsh = CreateObject("Wscript.Shell")
a = wsh.RegRead(MyRegKey)
Last edited by Dungeon Keeper; Mar 22nd, 2008 at 09:38 AM.
-
Mar 22nd, 2008, 10:23 AM
#2
Thread Starter
Fanatic Member
Re: How to check if registry key exists
Anyone? I need it urgently, please..
-
Mar 22nd, 2008, 10:26 AM
#3
Re: How to check if registry key exists
Be patient. Not everyone on this board is here at all hours, and not every can answer your question. As for myself, I'm researching an answer. If someone beats me to it, then so be it, but please do not be impatient.
-
Mar 22nd, 2008, 10:28 AM
#4
Thread Starter
Fanatic Member
-
Mar 22nd, 2008, 10:51 AM
#5
Re: How to check if registry key exists
Read about the RegCreateKeyEx API function here:
http://allapi.mentalis.org/apilist/RegCreateKeyEx.shtml
Read the section on the return value. This function returns a long, and you'll want to use the FormatMessage API function to interpret the result.
-
Mar 22nd, 2008, 11:03 AM
#6
Thread Starter
Fanatic Member
Re: How to check if registry key exists
 Originally Posted by Campion
Thanks, but i wanted the solution within the scope of WScript.Shell object.
Well i have my own solution for now but i dont want to use it:
I want to check directly if a reg key exists, not like this.
Code:
On Error GoTo MyErrors
dim a
a = 0
...
a = 1
MyValue = wsh.RegRead(MyRegKey)
'if program fails on line above it will jump to MyErrors
a = 2
...
...
:MyErrors
If a = 1 then MsgBox("Key doesnt exist") :D
Resume Next
Last edited by Dungeon Keeper; Mar 23rd, 2008 at 09:44 AM.
-
Mar 23rd, 2008, 10:21 AM
#7
Thread Starter
Fanatic Member
Re: How to check if registry key exists
If nobody knows any other way to do it, is it good to check like my example does?
-
Mar 23rd, 2008, 01:36 PM
#8
Re: How to check if registry key exists
You're better off using API's to get full information. WSH is a bit limited. However, as you want to use WSH, here are a few tips. This is for WSH and a REG_SZ - you'll have to do your own research for the other types.
1) If the subkey doesn't exist, you'll get an error of -2147024894. So rather than "Resume Next", which will hide any coding errors, you can do a [Select Case] with
Code:
MsgBox = Err.Number & " some suitable warning"
---------------------
2) When an empty REG_SZ value is created, the first 2 bytes it contains are nulls. So when this is read, you'll retreive an empty string. You can check this with Regedit by right clicking on the value name and choosing "Modify Binary Data".
When a non-empty REG_SZ value is created, the first 2 bytes will be whatever was written and a null (unicode). When this is read back, you'll get whatever was originally put there.
---------------------
3) When an empty REG_SZ has been created, but the first 2 bytes (nulls) have been deleted, WSH will return the Subkey/ValueName you passed to it. (This is because the registry is null delimited) The "Default" ValueName is usually in this state. You can test this with Regedit by right clicking on the Value Name, choosing "Modify Binary Data", deleting everything in there, then reading it.
---------------------
So there you have it.
(1) will tell you if the SubKey/ValueName is valid.
(2) will return the value.
(3) will tell you if the value is even initialised.
There are other scenarios, but outside the capabilities of WSH.
Last edited by schoolbusdriver; Mar 23rd, 2008 at 03:10 PM.
-
Mar 23rd, 2008, 01:46 PM
#9
Thread Starter
Fanatic Member
Re: How to check if registry key exists
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|