home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Programmer'…arterly (Limited Edition)
/
Visual_Basic_Programmers_Journal_VB-CD_Quarterly_Limited_Edition_1995.iso
/
code
/
ch29code
/
registry.bas
< prev
next >
Wrap
BASIC Source File
|
1995-08-02
|
4KB
|
71 lines
Attribute VB_Name = "Registry"
'********************************************************************
' REGSITRY.BAS - Contains the code necessary to access the Windows
' registration datbase.
'********************************************************************
#If Win32 Then
Option Explicit
'********************************************************************
' The minimal API calls required to read from the registry.
'********************************************************************
Private Declare Function RegOpenKey Lib "advapi32" Alias _
"RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, lpReserved As Long, lptype As Long, lpData As Any, _
lpcbData As Long) As Long
Private Declare Function RegCloseKey& Lib "advapi32" (ByVal hKey&)
'********************************************************************
' The constants used in this module for the regsitry API calls.
'********************************************************************
Private Const REG_SZ = 1 ' Unicode null terminated string
Private Const REG_EXPAND_SZ = 2 ' Unicode null terminated string
' with environment variable
' references.
Private Const ERROR_SUCCESS = 0
'********************************************************************
' The numeric constants for the major keys in the regsitry.
'********************************************************************
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
'********************************************************************
' GetRegString takes three arguments. A HKEY constant (listed above),
' a subkey, and a value in that subkey. This function returns the
' string stored in the strValueName value in the registry.
'********************************************************************
Public Function GetRegString(hKey As Long, strSubKey As String, _
strValueName As String) As String
Dim strSetting As String
Dim lngDataLen As Long
Dim lngRes As Long
'****************************************************************
' Open the key. If success, then get the data from the key.
'****************************************************************
If RegOpenKey(hKey, strSubKey, lngRes) = ERROR_SUCCESS Then
strSetting = Space(255)
lngDataLen = Len(strSetting)
'************************************************************
' Query the key for the current setting. If this call
' succeeds, then return the string.
'************************************************************
If RegQueryValueEx(lngRes, strValueName, ByVal 0, _
REG_EXPAND_SZ, ByVal strSetting, lngDataLen) = _
ERROR_SUCCESS Then
If lngDataLen > 1 Then
GetRegString = Left(strSetting, lngDataLen - 1)
End If
End If
'************************************************************
' ALWAYS close any keys that you open.
'************************************************************
If RegCloseKey(lngRes) <> ERROR_SUCCESS Then
MsgBox "RegCloseKey Failed: " & strSubKey, vbCritical
End If
End If
End Function
#End If