Discussion:
Getting Registry values
(too old to reply)
Bart Steur
2004-04-09 08:36:03 UTC
Permalink
Hello,

I want to retrieve the registry sections under a certain 'appname'. The
GetAllSettings(appname, section) allows me to retrieve all the keys within a
section, but now I need all sections within an appname. How can this be done
the easiest way.

Thanks,

Bart
Tom Esh
2004-04-09 13:13:29 UTC
Permalink
Post by Bart Steur
I want to retrieve the registry sections under a certain 'appname'. The
GetAllSettings(appname, section) allows me to retrieve all the keys within a
section, but now I need all sections within an appname. How can this be done
the easiest way.
You'll need to enumerate the subkeys under
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname

GetAllSettings only fetches valuenames and values You would need to
use the registry Api or a 3rd party component to enumerate the subkeys
within a given key. There are a number of free drop-in class modules
and components available. One of the more popular is at:
http://vbaccelerator.com/home/index.asp


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Bob Butler
2004-04-09 14:12:17 UTC
Permalink
Post by Tom Esh
Post by Bart Steur
I want to retrieve the registry sections under a certain 'appname'.
The GetAllSettings(appname, section) allows me to retrieve all the
keys within a section, but now I need all sections within an
appname. How can this be done the easiest way.
You'll need to enumerate the subkeys under
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname
GetAllSettings only fetches valuenames and values You would need to
use the registry Api or a 3rd party component to enumerate the subkeys
within a given key. There are a number of free drop-in class modules
http://vbaccelerator.com/home/index.asp
If WMI will be available then that may also be an option:

Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1
Private Const REG_EXPAND_SZ = 2
Private Const REG_BINARY = 3
Private Const REG_DWORD = 4
Private Const REG_MULTI_SZ = 7

Private Sub Main()
Dim oWMIReg As Object
Set oWMIReg = GetObject("winmgmts:root/default:StdRegProv")
EnumKey oWMIReg, HKEY_CURRENT_USER, "Software\VB and VBA Program Settings"
Set oWMIReg = Nothing
End Sub

Private Sub EnumKey(ByVal WMIReg As Object, ByVal HiveKey As Long, _
ByVal SubKey As String, Optional IndentLevel As Long)
Dim aEntries As Variant ' array of values/keys
Dim aTypes As Variant ' array of value types
Dim k As Long ' for enumerating
Debug.Print Space$(IndentLevel * 3) & SubKey
WMIReg.EnumValues HiveKey, SubKey, aEntries, aTypes
If IsArray(aEntries) Then
For k = LBound(aEntries) To UBound(aEntries)
Debug.Print Space$(IndentLevel * 3 + 3) & aEntries(k) & _
" [" & aTypes(k) & "]=" & _
GetRegValue(WMIReg, HiveKey, SubKey, aEntries(k), aTypes(k))
Next
End If
WMIReg.EnumKey HiveKey, SubKey, aEntries
If IsArray(aEntries) Then
For k = LBound(aEntries) To UBound(aEntries)
Debug.Print Space$(IndentLevel * 3 + 3) & aEntries(k)
EnumKey WMIReg, HiveKey, SubKey & "\" & aEntries(k), IndentLevel + 1
Next
End If
End Sub

Function GetRegValue(ByVal RegObject, ByVal HiveKey, ByVal SubKey, _
ByVal ValueName, ByVal ValueType)
Dim vValue, x
Select Case ValueType
Case REG_SZ, REG_EXPAND_SZ:
x = RegObject.GetStringValue(HiveKey, SubKey, ValueName, vValue)
Case REG_MULTI_SZ:
x = RegObject.GetMultiStringValue(HiveKey, SubKey, ValueName, vValue)
Case REG_BINARY:
x = RegObject.GetBinaryValue(HiveKey, SubKey, ValueName, vValue)
Case REG_DWORD:
x = RegObject.GetDWordValue(HiveKey, SubKey, ValueName, vValue)
End Select
If x = 0 Then GetRegValue = vValue
End Function

the only real problem I've found is that it always expands REG_EXPAND_SZ
values so if you need to get them in an unexpanded format you need to use
another method.
--
Reply to the group so all can participate
VB.Net... just say "No"
Tom Esh
2004-04-09 16:13:01 UTC
Permalink
Post by Bob Butler
...
the only real problem I've found is that it always expands REG_EXPAND_SZ
values so if you need to get them in an unexpanded format you need to use
another method.
Interesting. Never used it before, but but whew! Is WMI that slow by
nature? It took it about 10 secs to cough up maybe 50 lines debug
output on this P4 3ghz.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Bob Butler
2004-04-09 16:19:40 UTC
Permalink
Post by Tom Esh
Post by Bob Butler
...
the only real problem I've found is that it always expands
REG_EXPAND_SZ values so if you need to get them in an unexpanded
format you need to use another method.
Interesting. Never used it before, but but whew! Is WMI that slow by
nature? It took it about 10 secs to cough up maybe 50 lines debug
output on this P4 3ghz.
WMI is slower than other methods although that seems a lot slower than I'd
expect - on my system (P3 1.2) the code I posted runs very fast; I've been
using it a lot recently from VBScript since it's the easiest way to be able
to enumerate keys and values from there. From VB-proper I'd use the API but
thought I'd throw it out there for reference.
--
Reply to the group so all can participate
VB.Net... just say "No"
Rick Rothstein
2004-04-09 17:02:40 UTC
Permalink
Post by Bob Butler
Post by Tom Esh
Post by Bob Butler
...
the only real problem I've found is that it always expands
REG_EXPAND_SZ values so if you need to get them in an unexpanded
format you need to use another method.
Interesting. Never used it before, but but whew! Is WMI that slow by
nature? It took it about 10 secs to cough up maybe 50 lines debug
output on this P4 3ghz.
WMI is slower than other methods although that seems a lot slower than I'd
expect - on my system (P3 1.2) the code I posted runs very fast; I've been
using it a lot recently from VBScript since it's the easiest way to be able
to enumerate keys and values from there. From VB-proper I'd use the API but
thought I'd throw it out there for reference.
I was going to do a time test on my computer for comparison; however, your
program quit working midway through with a Runtime Error 13 - Type Mismatch.
The error occurs in the EnumKey subroutine on the GetRegValue function call
within the Debug.Print statement. I've not done WMI stuff before, so I don't
know what properties I can reference for the WMIReg object to report to you;
but the other arguments are as follows if it helps any

HiveKey = -2147483647
SubKey = Software\VB and VBA Program Settings\vbAdvance\Toolbar
aEntries(k) = ButtonData
aTypes(k) = 3

Rick - MVP
Bob Butler
2004-04-09 17:15:14 UTC
Permalink
"Rick Rothstein" <***@NOSPAMcomcast.net> wrote in message news:***@TK2MSFTNGP11.phx.gbl
<cut>
Post by Rick Rothstein
I was going to do a time test on my computer for comparison; however,
your program quit working midway through with a Runtime Error 13 -
Type Mismatch. The error occurs in the EnumKey subroutine on the
GetRegValue function call within the Debug.Print statement. I've not
done WMI stuff before, so I don't know what properties I can
reference for the WMIReg object to report to you; but the other
arguments are as follows if it helps any
HiveKey = -2147483647
SubKey = Software\VB and VBA Program Settings\vbAdvance\Toolbar
aEntries(k) = ButtonData
aTypes(k) = 3
The GetRegValue is returning a variant with the data and in the case of a
REG_BINARY value that is a byte array which the Debug.Print chokes on. I
didn't happen to have any binary values on this system so I did not think to
check for that. I separated out the call and check the return type in this
version:

Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1
Private Const REG_EXPAND_SZ = 2
Private Const REG_BINARY = 3
Private Const REG_DWORD = 4
Private Const REG_MULTI_SZ = 7

Private Sub Main()
Dim oWMIReg As Object
Set oWMIReg = GetObject("winmgmts:root/default:StdRegProv")
EnumKey oWMIReg, HKEY_CURRENT_USER, "Software\VB and VBA Program Settings"
Set oWMIReg = Nothing
End Sub

Private Sub EnumKey(ByVal WMIReg As Object, ByVal HiveKey As Long, _
ByVal SubKey As String, Optional IndentLevel As Long)
Dim aEntries As Variant ' array of values/keys
Dim aTypes As Variant ' array of value types
Dim vData As Variant
Dim k As Long ' for enumerating
Debug.Print Space$(IndentLevel * 3) & SubKey
WMIReg.EnumValues HiveKey, SubKey, aEntries, aTypes
If IsArray(aEntries) Then
For k = LBound(aEntries) To UBound(aEntries)
vData = GetRegValue(WMIReg, HiveKey, SubKey, aEntries(k), aTypes(k))
If IsArray(vData) Then vData = "{array}" ' just to protect the
Debug.Print
Debug.Print Space$(IndentLevel * 3 + 3) & aEntries(k) & _
" [" & aTypes(k) & "]=" & vData
Next
End If
WMIReg.EnumKey HiveKey, SubKey, aEntries
If IsArray(aEntries) Then
For k = LBound(aEntries) To UBound(aEntries)
Debug.Print Space$(IndentLevel * 3 + 3) & aEntries(k)
EnumKey WMIReg, HiveKey, SubKey & "\" & aEntries(k), IndentLevel + 1
Next
End If
End Sub

Function GetRegValue(ByVal RegObject, ByVal HiveKey, ByVal SubKey, _
ByVal ValueName, ByVal ValueType)
Dim vValue, x
Select Case ValueType
Case REG_SZ, REG_EXPAND_SZ:
x = RegObject.GetStringValue(HiveKey, SubKey, ValueName, vValue)
Case REG_MULTI_SZ:
x = RegObject.GetMultiStringValue(HiveKey, SubKey, ValueName, vValue)
Case REG_BINARY:
x = RegObject.GetBinaryValue(HiveKey, SubKey, ValueName, vValue)
Case REG_DWORD:
x = RegObject.GetDWordValue(HiveKey, SubKey, ValueName, vValue)
End Select
If x = 0 Then GetRegValue = vValue
End Function

On my system it runs too fast to bother timing; it's a fraction of a second.
--
Reply to the group so all can participate
VB.Net... just say "No"
Tom Esh
2004-04-09 21:09:31 UTC
Permalink
Post by Bob Butler
WMI is slower than other methods although that seems a lot slower than I'd
expect - on my system (P3 1.2) the code I posted runs very fast; I've been
using it a lot recently from VBScript since it's the easiest way to be able
to enumerate keys and values from there. From VB-proper I'd use the API but
thought I'd throw it out there for reference.
I hear ya. The pessimist in me filed it in case I ever have to do
script. <g>


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)

Loading...