mooose
Jan 23rd, 2000, 06:24 AM
Howdy everyone
ok the first question I don't really know how to explain so I'll have to use an example:
ok say I was getting my program to search the hard drive for a file say mooose.ocx ok now say it came back with 50 matches and I wanted it to write this data to the registry
I write to the registry like so:
SetStringValue "HKEY_LOCAL_MACHINE\software\moose inc\what ever the programs name", "title of string", "then the string" so instead of make 50 if statements to cope with 50 matches is there someway to just make 1 if statement that will write how many ever files are found paths to the registry?
so it would look like this:
SetStringValue "HKEY_LOCAL_MACHINE\software\moose inc\search", "match1", "C:\programs files\mooose.ocx"
SetStringValue "HKEY_LOCAL_MACHINE\software\moose inc\search", "match2", "C:\programs files\mooose\mooose.ocx"
and so on just adding another number to the match and listing the path like above?
and also a slight variation of the question above so to read from the registry I would read from it like so: GetStringValue("HKEY_LOCAL_MACHINE\software\Mooose Inc\search", "match")
so what i want to know also is how would I get say all the values out of the registry into a list box so instead of having to type another 50 statements to get each file. so how would i do those things?
thanx
------------------
Mooose
Mark Sreeves
Jan 23rd, 2000, 05:04 PM
Here you are Moose:
Form Code
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
For i = 0 To 5
SaveValue "match" & i, "something " & i
Next i
End Sub
Private Sub Command2_Click()
Dim i As Integer
Dim KeyCount As Long
Dim Names() As String
Dim Values() As String
KeyCount = EnumRegValue(HKEY_LOCAL_MACHINE, ROOT_REG_KEY, Names, Values)
For i = 0 To KeyCount - 1
List1.AddItem Names(i) & " " & Values(i)
Next i
End Sub
Module Code
[code]
Option Explicit
Public Const ROOT_REG_KEY As String = "software\moose inc\search\"
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CURRENT_USER = &H80000001
Public Const REG_SZ = 1 ' null terminated string
Public Const INVALID_HANDLE_VALUE = -1
Public Const ERROR_SUCCESS = 0&
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public 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))
Public Const MY_GENERAL_ACCESS = KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS
Public Const ERROR_NO_MORE_ITEMS = 259&
Public Const SRCCOPY = &HCC0020
' System registry functions
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public 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
Public Declare Function RegQueryValueExStr Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Public Enum ErrorCodes
GeneralError = vbObjectError Or 1
errJetMin = 2420
errJetMax = 3629
End Enum
Public Function ReadValue(ByVal Key As String) As String
Dim RegKey As Long
RegKey = OpenRegKey(HKEY_LOCAL_MACHINE, ROOT_REG_KEY)
' Get the value and return it
ReadValue = ReadRegValue(RegKey, Key)
' Close the registry
CloseRegKey (RegKey)
End Function
Public Sub CloseRegKey(ByRef RegKey As Long)
' Close the registry key
'
' Set the error handler
On Error Resume Next
If IsValidHandle(RegKey) Then
RegCloseKey (RegKey)
RegKey = INVALID_HANDLE_VALUE
End If
End Sub
Public Function IsValidHandle(ByVal Handle As Long) As Boolean
' Check a handle to see if it is valid
'
' @return: Boolean indication of a valid hanndle
'
' Set the error handler
On Error Resume Next
IsValidHandle = Not (Handle = INVALID_HANDLE_VALUE)
End Function
Public Sub SaveValue(ByVal Key As String, ByVal Value As String)
' Open the registry
Dim RegKey As Long
RegKey = OpenRegKey(HKEY_LOCAL_MACHINE, ROOT_REG_KEY)
' Set the value
WriteRegValue RegKey, Key, Value
' Close the registry
CloseRegKey (RegKey)
End Sub
Public Function ReadRegValue(ByVal Key As Long, ByVal ValueName As String) As String
' Read a value out of the registry
'
' Set the error handler
On Error Resume Next
Dim Buffer As String
Dim Length As Long
Length = 255
Buffer = String(Length, vbNullChar)
If RegQueryValueExStr(Key, ValueName, 0, REG_SZ, Buffer, Length) = ERROR_SUCCESS Then
ReadRegValue = Left(Buffer, Length - 1)
Else
ReadRegValue = ""
End If
End Function
Public Sub WriteRegValue(ByVal Key As Long, ByVal ValueName As String, ByVal Data As String)
' Write a value into the registry
'
' Set the error handler
On Error GoTo WriteRegValueErr
' Get the length of the data and append a null terminator
Dim Length As Long
Length = Len(Data) + 1
Data = Data & vbNullChar
' Set the value
If RegSetValueEx(Key, ValueName, 0, REG_SZ, ByVal Data, Length) <> ERROR_SUCCESS Then
Err.Raise 42, "WriteRegValue", "Unable to set registry value"
End If
WriteRegValueExit:
Exit Sub
WriteRegValueErr:
Err.Raise Err.Number, Err.Source, Err.Description
End Sub
Public Function OpenRegKey(ByVal Root As Long, ByVal Path As String) As Long
' Open the specified registry key and return the key.
'
' @param: Root - The key of an open root branch
' @param: Path - The path to the branch to open
'
' Set the error handler
On Error Resume Next
Dim MyRegKEy As Long
If RegOpenKeyEx(Root, Path, 0, MY_GENERAL_ACCESS, MyRegKEy) = ERROR_SUCCESS Then
OpenRegKey = MyRegKEy
Else
OpenRegKey = INVALID_HANDLE_VALUE
End If
End Function
Public Function EnumRegValue(ByVal Root As Long, ByVal Path As String, ByRef Names() As String, ByRef Values() As String) As Long
' Enumerate the values of a key in the registry.
'
' @param: Root - The root key that the path is descended from
' @param: Path - The path to the key to enumerate
' @param: Values() - The list to return the values in
' @return: Count of the number of values
'
' Set the error handler
On Error GoTo EnumRegValueErr
' Open the registry key for enumeration
Dim MyKey As Long
If RegOpenKeyEx(Root, Path, 0, KEY_QUERY_VALUE, MyKey) <> ERROR_SUCCESS Then
Err.Raise GeneralError
End If
Dim Counter As Long
Dim NameLength As Long
Dim ValueLength As Long
Dim TypeCode As Long
Dim NameBuffer As String
Dim ValueBuffer As String
Dim Result As Long
Counter = 0
EnumLoop:
NameLength = 255
ValueLength = 1024
NameBuffer = String(NameLength, vbNullChar)
ValueBuffer = String(ValueLength, vbNullChar)
' Enumerate the key
Result = RegEnumValue(MyKey, Counter, NameBuffer, NameLength, 0, TypeCode, ByVal ValueBuffer, ValueLength)
' If it worked then add the information and repeat
If Result = ERROR_SUCCESS And TypeCode = REG_SZ Then
' Store the name and value
ReDim Preserve Names(Counter)
ReDim Preserve Values(Counter)
Names(Counter) = Left(NameBuffer, NameLength)
Values(Counter) = Left(ValueBuffer, ValueLength)
DiGiTaIErRoR
Jan 23rd, 2000, 05:13 PM
Why use the registry?
try this code:
---
In a command button
List1.Clear 'Files put here
List2.AddItem "C:\" 'Invisible list of dirs
Do
findfilesdir lstdirs.List(0), "moose.ocx" 'search pattern
List2.RemoveItem 0
Loop Until List2.ListCount = 0
In the forms general | declaration
Public Sub findfilesdir(DirPath As String, FileSpec As String)
Dim filestring As String
Dim SearchFileName As String
DirPath = Trim$(DirPath)
If Right$(DirPath, 1) <> "\" Then
DirPath = DirPath & "\"
End If
filestring = Dir$(DirPath & FileSpec, vbArchive Or vbHidden Or vbSystem Or vbDirectory)
Do
DoEvents
If filestring = "" Then
Exit Do
Else
If (GetAttr(DirPath & filestring) And vbDirectory) = vbDirectory Then
If Left$(filestring, 1) <> "." And Left$(filestring, 2) <> ".." Then
List2.AddItem DirPath & filestring & "\", 1
End If
Else
SearchFileName = DirPath & filestring
End If
End If
If InStr(SearchFileName, Text1.Text) Then List1.AddItem SearchFileName
filestring = Dir$
Loop
End Sub
Good luck!
P.S.This was taken from this site 'tweaked' a litle bit.
------------------
DiGiTaIErRoR