Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RegEnumKeyEx API Function

118 views
Skip to first unread message

Adrian M

unread,
Oct 27, 1998, 3:00:00 AM10/27/98
to

Teoh Teik Liang wrote in message <3636343A...@hotmail.com>...
>Have anyone use the RegEnumKeyEx API function before ? I need for
>example to understand how to use it. Any help for me ?


Here's an example using enumeration DSNs that are installed on the system.
It uses RegEnumKeyEx:

Place the following code in a class called RegEnum:

'API Declares
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
Alias "RegOpenKeyExA" (ByVal hKey As Long, _
ByVal lpsSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, phkResult As Long) As Long

Private Declare Function RegEnumKeyEx Lib "advapi32.dll" _
Alias "RegEnumKeyExA" (ByVal hKey As Long, _
ByVal dwIndex As Long, ByVal lpName As String, _
lpcbName As Long, lpReserved As Long, _
ByVal lpClass As String, lpcbClass As Long, _
lpftLastWriteTime As Any) As Long

' Note that if you declare the lpData parameter as String,
'you must pass it ByVal
Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, lpData As Any, lpcbData As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long

'Registry constants
Private Const ERROR_SUCCESS = 0&

'Reg Key Security constants
Private Const DELETE = &H10000
Private Const READ_CONTROL = &H20000
Private Const WRITE_DAC = &H40000
Private Const WRITE_OWNER = &H80000
Private Const SYNCHRONIZE = &H100000
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const SPECIFIC_RIGHTS_ALL = &HFFFF
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or
KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or
KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or
KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not
SYNCHRONIZE))
Private Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))

'Constants for Registry Hives...
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003
Private Const HKEY_PERFORMANCE_DATA = &H80000004
Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const HKEY_DYN_DATA = &H80000006
'_____________________________________________________________________

Public Enum MainHives
mhHkeyClassesRoot = HKEY_CLASSES_ROOT
mhHkeyCurrentUser = HKEY_CURRENT_USER
mhHkeyLocalMachine = HKEY_LOCAL_MACHINE
mhHkeyUsers = HKEY_USERS
mhHkeyPerformanceData = HKEY_PERFORMANCE_DATA
mhHkeyCurrentConfig = HKEY_CURRENT_CONFIG
mhHkeyDynData = HKEY_DYN_DATA
End Enum


Public Sub EnumKey(mhIn As MainHives, sSubKey As String, Keys() As
String)

Dim sBuffer As String
Dim lRet As Long
Dim ldx As Long
Dim lBuflen As Long
Dim hKey As Long
Dim sFileTime As String

sBuffer = Space$(1024)
lRet = RegOpenKeyEx(mhIn, sSubKey, 0&, KEY_ALL_ACCESS, hKey)
If lRet = ERROR_SUCCESS Then

' Enumerate keys

Do
lBuflen = Len(sBuffer)
lRet = RegEnumKeyEx(hKey, ldx, sBuffer, lBuflen, _
ByVal 0, vbNullString, ByVal 0, sFileTime)
If lRet = ERROR_SUCCESS Then
'keep each element typed...don't want variants
ReDim Preserve Keys(0 To ldx) As String
Keys(ldx) = Left(sBuffer, lBuflen)
ldx = ldx + 1
Else
Exit Do
End If
Loop
Call RegCloseKey(hKey)

End If

End Sub


Now on a form with a command button (or basically whereever) place
this code:

Private Sub Command1_Click()

Dim s() As String
Dim l As Long
Dim amReg As RegEnum
Dim sSubKey As String

Set amReg = New RegEnum
sSubKey = "SOFTWARE\ODBC\odbc.ini"

amReg.EnumKey mhHkeyLocalMachine, sSubKey, s

For l = 0 To UBound(s)
Debug.Print s(l)
Next
End Sub


;-) Cool
Hasta Luego
Adrian Maull, MCP
(author of the amDeveloper Bar
www.middlebay.com/amware)


Teoh Teik Liang

unread,
Oct 28, 1998, 3:00:00 AM10/28/98
to

Adrian M

unread,
Oct 28, 1998, 3:00:00 AM10/28/98
to

Teoh Teik Liang wrote in message <36377B2A...@hotmail.com>...
>I would like to thanks for the example that you gave me. It help me a lot.
>
>RegEnumKeyEx function is only use to query the registry 's key isn't it ?
how
>about I want to query the value key ? What function is use to query Value
Key
>in the registry? Also, please provide example for me. Thanks.


Head over to http://www.mvps.org/vb/ and check out the SysSnd.Zip sample.
It gives a good example of how to enum key values...

Teoh Teik Liang

unread,
Oct 29, 1998, 3:00:00 AM10/29/98
to

Fernndo Pacheco

unread,
Oct 30, 1998, 3:00:00 AM10/30/98
to
Hi,

I had some problems using RegEnumKeyEx too.
Running my program from the VB environment it works
fine, but if I run the compiled version it returns
ERROR_MORE_DATA and the var sBuffer is empty.
Here's where he problem is : (the whole program follows)

...

ERROR_MORE_DATA = 234
...

lRet = RegEnumKeyEx(hKey, ldx, sBuffer, lBuflen, _
ByVal 0, vbNullString, ByVal 0, sFileTime)
If lRet = ERROR_SUCCESS Then

...

Thanks,
Fern...@telegoias.com.br

----------------------


Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal
ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal
lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long


' Note that if you declare the lpData parameter as String, you must pass it

By Value.
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long)
As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias
"RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As
String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String,
lpcbClass As Long, lpftLastWriteTime As Any) As Long

Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003
Private Const HKEY_PERFORMANCE_DATA = &H80000004
Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const HKEY_DYN_DATA = &H80000006

Private Sub Form_Load()
If getSW = False Then
List1.AddItem "*** NENHUM SOFTWARE INSTALADO ***"
End If
End Sub

Public Function getSW() As Boolean
Dim l As Long, j As Long
Dim si As String, strPrg As String
Dim sSubKey As String
getSW = False
sSubKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
x = EnumKey("HKEY_LOCAL_MACHINE", sSubKey)
For i = 1 To Len(x) / 100
si = Trim(Mid(x, 100 * (i - 1) + 1, 100))
strPrg = GetSetting("HKEY_LOCAL_MACHINE", sSubKey & "\" & si,
"DisplayName", "")
If strPrg <> "" Then
List1.AddItem strPrg
getSW = True
End If
Next
End Function

Public Function EnumKey(ByVal HK As String, ByVal sSubKey As String) As
String


Dim sBuffer As String
Dim lRet As Long
Dim ldx As Long
Dim lBuflen As Long
Dim hKey As Long
Dim sFileTime As String

Dim fmtx As String
Dim enK As String
fmtx =
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@!"
enK = ""
sBuffer = Space$(10240)
lRet = RegOpenKeyEx(GetHkey(HK), sSubKey, 0&, KEY_ALL_ACCESS, hKey)


If lRet = ERROR_SUCCESS Then
' Enumerate keys

ldx = 0
Do
lBuflen = Len(sBuffer)
lRet = RegEnumKeyEx(hKey, ldx, ByVal sBuffer, lBuflen, _


ByVal 0, vbNullString, ByVal 0, sFileTime)

If lRet = ERROR_SUCCESS Then 'QUANDO COMPILADO LRET TEM OUTRO
VALOR


ldx = ldx + 1

enK = enK & Format(Left(sBuffer, lBuflen), fmtx)


Else
Exit Do
End If
Loop
Call RegCloseKey(hKey)
End If

EnumKey = enK
End Function

Public Function GetSetting(ByVal HK As String, ByVal Section As String,
ByVal Key As String, Optional ByVal Default As String = "") As String
Dim nRet As Long
Dim hKey As Long
Dim nType As Long
Dim nBytes As Long
Dim Buffer As String

GetSetting = Default

nRet = RegOpenKeyEx(GetHkey(HK), Section, 0&, KEY_ALL_ACCESS, hKey)
If nRet = ERROR_SUCCESS Then
If Key = "*" Then Key = vbNullString

nRet = RegQueryValueEx(hKey, Key, 0&, nType, ByVal Buffer, nBytes)
If nRet = ERROR_SUCCESS Then
' Build buffer and get data
If nBytes > 0 Then
Buffer = Space(nBytes)
nRet = RegQueryValueEx(hKey, Key, 0&, nType, ByVal Buffer,
Len(Buffer))
If nRet = ERROR_SUCCESS Then
' Trim NULL and return successful query!
GetSetting = Left(Buffer, nBytes - 1)
End If
End If
End If
End If
End Function

Private Function GetHkey(ByVal HK) As Variant
Select Case HK
Case "HKEY_CLASSES_ROOT"
GetHkey = HKEY_CLASSES_ROOT
Case "HKEY_CURRENT_USER"
GetHkey = HKEY_CURRENT_USER
Case "HKEY_LOCAL_MACHINE"
GetHkey = HKEY_LOCAL_MACHINE
Case "HKEY_USERS"
GetHkey = HKEY_USERS
Case "HKEY_PERFORMANCE_DATA"
GetHkey = HKEY_PERFORMANCE_DATA
Case "HKEY_CURRENT_CONFIG"
GetHkey = HKEY_CURRENT_CONFIG
Case "HKEY_DYN_DATA"
GetHkey = HKEY_DYN_DATA
Case Else
GetHkey = &H0
End Select
End Function


Fernndo Pacheco

unread,
Oct 30, 1998, 3:00:00 AM10/30/98
to

Fernndo Pacheco

unread,
Oct 30, 1998, 3:00:00 AM10/30/98
to
Hi,

I had some problems using RegEnumKeyEx too.
Running my program from the VB environment it works
fine, but if I run the compiled version it returns
ERROR_MORE_DATA and the var sBuffer is empty.
Here's where he problem is : (the whole program follows)

...

ERROR_MORE_DATA = 234
...

lRet = RegEnumKeyEx(hKey, ldx, sBuffer, lBuflen, _
ByVal 0, vbNullString, ByVal 0, sFileTime)
If lRet = ERROR_SUCCESS Then
...

-----------------------------

GetSetting = Default

------------------------------

Thanks,
Fern...@telegoias.com.br

----------------------

Fernando Pacheco

unread,
Oct 30, 1998, 3:00:00 AM10/30/98
to
Solution!!

change from:
Dim sFileTime As String

to:
Dim sFileTime As Currency

----------------------------------
Fernndo Pacheco wrote in message ...

0 new messages