|
|||||||
|
|
|
|||||
|
|
|||||||
How to read values from the Windows Registry, to obtain details of the settings on the computer? Try . . .
Calling DZGetRegString
This example shows the use of DZGetRegString to return the value for the
Excel extension, .xls.
Dim wrkString As String
'
' Get Registration Name for Excel Worksheet (.xls File)
wrkString = DZGetRegString(".xls")
MsgBox wrkString
Note : A valid version of Excel will return something
like Excel.Sheet.8, when the routine DZGetRegString tests the Windows Registry with .xls.
Note that 8 identifies Excel 97.
This routine only works for 32-bit versions of Visual Basic.
It has been updated with code for the key REG_EXPAND_SZ, which tends to get
used in NT.
DZGetRegString
The routine, DZGetRegString, tests the Windows Registry for a particular
value.
It is actually a collection of routines, that has been simplified here, by
removing the code used for 16-bit versions of Daisy.
Public Function DZGetRegString(argKeyValue as String) As String
On Error Resume Next
DZGetRegString = QueryValue(HKEY_LOCAL_MACHINE, _
"SOFTWARE\Classes\" & argKeyValue, "")
End Function
Public Function QueryValue(sKeyArea As Long, sKeyName As String, _
sValueName As String) As Variant
Dim lRetVal As Long
Dim hKey As Long
Dim vValue As Variant
lRetVal = RegOpenKeyEx(sKeyArea, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = QueryValueEx(hKey, sValueName, vValue)
If (lRetVal <> ERROR_NONE) Then vValue = ""
RegCloseKey (hKey)
QueryValue = vValue
End Function
Function QueryValueEx(ByVal lhKey As Long, _
ByVal szValueName As String, vValue As Variant) As Long
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String
Dim StrLen As Integer
On Error GoTo QueryValueExError
'
' Determine the size and type of data to be read
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
If (lrc <> ERROR_NONE) Then Error 5
Select Case lType
'
' For strings
Case REG_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
sValue, cch)
If (lrc = ERROR_NONE) Then
StrLen = lstrlen(sValue)
vValue = Left$(sValue, StrLen)
Else
vValue = Empty
End If
'
' For strings
Case REG_EXPAND_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, _
sValue, cch)
If (lrc = ERROR_NONE) Then
sValue = Left$(sValue, cch - 1)
sValue2 = String(cch + 400, 0)
lrc2 = ExpandEnvironmentStrings(sValue, sValue2, cch + 400)
If (lrc2 > 0) Then
vValue = Left$(sValue2, lrc2)
Else
vValue = Empty
End If
Else
vValue = Empty
End If
'
' For DWORDS
Case REG_DWORD:
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, _
lValue, cch)
If (lrc = ERROR_NONE) Then vValue = lValue
Case Else
'
' all other data types not supported
lrc = -1
End Select
'
' Errors
QueryValueExExit:
QueryValueEx = lrc
Exit Function
QueryValueExError:
Resume QueryValueExExit
End Function
Note : The code in these routines has been assembled from
various sources, such as Microsoft.
There are also a series of definitions needed, which come from Windows API.
Global Const REG_SZ As Long = 1
Global Const REG_EXPAND_SZ As Long = 2
Global Const REG_DWORD As Long = 4
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const ERROR_NONE = 0
Global Const KEY_ALL_ACCESS = &H3F
Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
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
Declare Function RegQueryValueExString 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
Declare Function RegQueryValueExLong Lib "advapi32.dll" _
Alias "RegQueryValueExA" _
(ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
lpData As Long, _
lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" _
Alias "RegQueryValueExA" _
(ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
ByVal lpData As Long, _
lpcbData As Long) As Long
Declare Function ExpandEnvironmentStrings Lib "kernel32" _
Alias "ExpandEnvironmentStringsA" _
(ByVal lpSrc As String, _
ByVal lpDst As String, _
ByVal nSize As Long) As Long
This routine is very important and can be used in lots of places. So check
it well!
Copyright (c) 1999 - 2001, robert han, all rigths are reserved.