Results 1 to 9 of 9

Thread: How to check if registry key exists

  1. #1

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    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.

  2. #2

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to check if registry key exists

    Anyone? I need it urgently, please..

  3. #3
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    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.

  4. #4

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to check if registry key exists

    Im always patient but this is urgent

    Sorry if i am bothering

    Ill be more patient then

  5. #5
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    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.

  6. #6

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to check if registry key exists

    Quote Originally Posted by Campion
    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.
    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.

  7. #7

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    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?

  8. #8
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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.

  9. #9

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    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
  •  



Click Here to Expand Forum to Full Width