
Is this possible, or am I pushing it?
Quote:
> Hello,
> I am using VB5 Enterprise to construct an FTP client. I want to be
> able to save session profiles to the registry, which is no problem. I
> have it saving the profiles to the registry in the following manner:
> HKEY_CURRENT_USER\MyPrograms\MyFTP\Profiles
> and in there, each individual profile has it's own subkey, for
> instance:
> HKEY_CURRENT_USER\MyPrograms\MyFTP\Profiles
> MyProfile1
> MyProfile2
> MyProfile3
> Now, the thing that I want to do with this is have EACH individual
> profile subkey load into a combo box, and when I select the subkey
> name from the combo box, have all of the settings load into various
> text boxes on my form.
> As I said, SAVING the profiles to the registry is not a problem. But
> loading the subkeys from the registry in to the combo box has me
> spending MANY speepless nights trying to figure it out. If anyone can
> help me on this, THANKS A MILLION!! =)
The function you're looking for is RegEnumKeyEx(). It's declared as
Declare Function RegEnumKeyEx& Lib "advapi32.dll" Alias "RegEnumKeyExA" _
(ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, _
lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As _
String, lpcbClass As Long, lpftLastWriteTime As FILETIME)
The other declarations you'll need are the FILETIME type, ERROR_NONE,
HKEY_CURRENT_USER and KEY_ALL_ACCESS, plus the RegOpenKeyEx and
RegCloseKey functions; you can get these through the API viewer.
The code you need to use is this:
Public Function EnumSubKey(ByVal lIndex as Long) as String
Dim y as Long
Dim hKey as Long
Dim sKeyBuffer as String
Dim sSubKey as String
Dim lKeyBufferLen as Long
Dim foo as Long
Dim FT as FILETIME
sKeyBuffer = String$(256, vbNullChar) 'allocate a buffer for the
'response
lKeyBufferLen = Len(sKeyBuffer)
foo = Len(vbNullString)
' open the key
y = RegOpenKeyEx(HKEY_CURRENT_USER, "MyPrograms\MyFTP\Profiles", _
0&, KEY_ALL_ACCESS, hKey)
If y = ERROR_NONE Then
'here's the meat. hKey is the handle to the Profiles key. lIndex
'is the zero-based index of the subkey. sKeyBuffer is the buffer
'you've allocated; lKeyBufferLen is its length. You don't need to
'worry about the other parameters.
y = RegEnumKeyEx(hKey, lIndex, sKeyBuffer, lKeyBufferLen, _
0&, vbNullString, foo, FT)
If y = ERROR_NONE Then
'sKeyBuffer is a null-terminated string containing the subkey's
'name. lKeyBufferLen is set to the length of the key name incl.
'the null character.
sSubKey = Left$(sKeyBuffer, lKeyBufferLen - 1)
Else
sSubKey = ""
End If
RegCloseKey hKey
End If
EnumSubKey = sSubKey
End Function
Then add the following to the code you're using to populate the combo
box:
Dim sSubKey as String
Dim lKeyIndex as Long
lKeyIndex = 0
Do
sSubKey = EnumSubKey(lKeyIndex)
If sSubKey <> "" Then Combo1.AddItem sSubKey
lKeyIndex = lKeyIndex + 1
Loop Until sSubKey = ""
--