Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Registry Keys 2

Status
Not open for further replies.

Bluejay07

Programmer
Mar 9, 2007
780
0
0
CA
Hello,

I am looking for a method to loop through the registry and obtain a list of keys. I know that GetAllSettings exist, although I think this only accessing the default VB And VBA Settings folder. I've searched and the enumeration examples I've found don't really seem to work except for locating the top level folders listed under Hkey_Local_Machine

I am looking for the following:
Hkey_Local_Machine\Software\CompName\Prog

Prog could have keys (String Value) A, B, C, D.
These are the values I am trying to return.

Thanks.




If at first you don't succeed, then sky diving wasn't meant for you!
 
You may want to look at the RegOpenKeyExA and RegEnumKey API calls. Here's a code fragment
Code:
Function GetValues() As String()

    ' Declares, etc.

    Const HKEY_LOCAL_MACHINE = &H80000002
    MainKey = HKEY_LOCAL_MACHINE
    SubKey = "Software\CompName\Prog\"
    ' Open the key
    Ret = RegOpenKeyExA(MainKey, SubKey, 0&, KEY_READ, lpHKey)
    If Ret <> ERROR_SUCCESS Then
        Exit Function   'No key open, so leave
    End If

    ' Set up a buffer to receive returned data.
    ' Adjust next value for larger buffers.
    lpcbData = 255
    ReturnedString = Space$(lpcbData)

    ' Loop through the keys until RegEnumKey returns ERROR_NO_MORE_ITEMS (259)
    N = 0
    Ret = RegEnumKey(lpHKey, CLng(N), ReturnedString, lpcbData)
    Do Until Ret = ERROR_NO_MORE_ITEMS Or Ret <> ERROR_SUCCESS
        ReDim Preserve Values(N)
        Values(N) = Left$(ReturnedString, InStr(1, ReturnedString, Chr$(0)) - 1)
        N = N + 1
        Ret = RegEnumKey(lpHKey, CLng(N), ReturnedString, lpcbData)
    Loop

    Ret = RegCloseKey(lpHKey)
    GetValues = Values

End Function
 
Hi Golom,
Thank you for the response and I also appreciate the code snippet. I will try the above and see how it turns out.

Have a great day.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Here's an alternative using WMI. Whilst it has a slight performance overhead it has the advantage that it can read registries on remote workstations ...
Code:
[blue]Function GetValues() As Variant
    Const HKEY_LOCAL_MACHINE = &H80000002
    
    Dim strKeyPath As String
    Dim arrValueNames As Variant

    strKeyPath = "SYSTEM\CurrentControlSet\Control\Lsa"
    
    strComputer = "."
    Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

    objReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames, arrValueTypes
    GetValues = arrValueNames
    
End Function[/blue]
 
Thank you for providing an alternative method. Two options is better than none.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Oh, you'll want to change

strKeyPath = "SYSTEM\CurrentControlSet\Control\Lsa"

to

strKeyPath = "Software\CompName\Prog\"


(or whatever)
 
@Golom:
How should mainkey, subkey (string, I'm assuming) and any other missing variable (such as KEY_Read, lpHKey, Ret, lpcpdata, ReturnedString (string I'm assuming) etc.) be declared?

@strongm:
I couldn't get your code working. I added:
Dim arrValueTypes as variant,
Dim strComputer As String,
Dim objReg as object.

I changed the path (thanks to your second post) and arrValueNames does not seem to have any data (testing in immediate windows with ?arr_ValueName)

Am I missing something in either case?

If at first you don't succeed, then sky diving wasn't meant for you!
 
> I added:


Oops. Yes, I quickly threw this together from some VBScript code I had written, and failed to put in all the necessary declarations. However, you've selected suitable declarations there.

However, by function (me being foolish) returns values from a key, not subkeys - which is what you really want

Here's the subkey version (with apprpriate declarations this time). One again you'll want to change strKeyPath to your own value:
Code:
[blue]Public Function GetKeys() As Variant
    Const HKEY_LOCAL_MACHINE = &H80000002
    Dim strComputer As String
    Dim objReg As Object
    
    Dim strKeyPath As String
    Dim arrValueNames As Variant

    strKeyPath = "SOFTWARE\Adobe\"
    
    strComputer = "."
    Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

    objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrValueNames   ', arrValueTypes
    GetKeys = arrValueNames
End Function[/blue]
 
Variables are declared as
Code:
Dim Ret                As Long     'Returned by registry functions
Dim lpHKey             As Long     'Return handle to opened key
Dim lpcbData           As Long     'Length of data in returned string
Dim ReturnedString     As String   'Returned string value
Dim N                  As Long     'Loop counter
Dim MainKey            As Long     'Key for the primary registry entry
Dim SubKey             As String   'Specific directory sub-key
Dim Values()           As String   'Array of values found

Const ERROR_SUCCESS = 0&
Const ERROR_NO_MORE_ITEMS = 259&

Const READ_CONTROL = &H20000
Const STANDARD_RIGHTS_READ = READ_CONTROL
Const KEY_QUERY_VALUE = &H1
Const KEY_ENUMERATE_SUB_KEYS = &H8&
Const KEY_NOTIFY = &H10&
Const KEY_READ =    STANDARD_RIGHTS_READ _
                 Or KEY_QUERY_VALUE _
                 Or KEY_ENUMERATE_SUB_KEYS _
                 Or KEY_NOTIFY



 
Thank you both for posting the additional code.
Both sets of code seems to work, however, Strongm, I believe you were correct in the first instance of what I am trying to achieve. If I miscommunicated, I appologize.

Here is the structure I am trying to achieve:
My Computer
+ --- HKEY_LOCAL_MACHINE
+ --- SOFTWARE
+ --- Company
+ --- Source
(values reside here)
Name Type Data
A REG_SZ ""
B REG_SZ ""
C REG_SZ ""
D REG_SZ ""

The above codes return 'Source' if the path is: Software\Company. I am trying to obtain the string values A, B, C, D.

Once again I appreciate your feedback and I appologize if I have miscommunicated.


If at first you don't succeed, then sky diving wasn't meant for you!
 
Well, in that case I can't tell you why my original code GetValues (plus your additional declarations) does not work when

strKeyPath = "Software\CompName\Prog\"

It certainly works here against test data that matches your values


 
I tried again and this time it seems to work. Maybe I missed something the first time around. At least I can now navigate the subkeys and the values.

Thank you!

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top