CSDN博客

img ronggang

注册表操作类

发表于2004/10/6 17:47:00  732人阅读

分类: 通用函数

'************************************************
'** 类 名 称:  RegInfos
'** 作    者:  ronggang(ronggang@5ivb.net)
'** 创建时间:  2004-09-07
'** 最后修改:  2004-09-07
'** 说    明:  实现基本的注册表操作
'************************************************

Private Const REG_SZ                As Long = 1                   ' 字符串
Private Const REG_BINARY            As Long = 3                   ' 二进制
Private Const REG_DWORD             As Long = 4                   ' 数值(十六进制、┼进制)
Private Const HKEY_CURRENT_USER     As Long = &H80000001
Private Const HKEY_LOCAL_MACHINE    As Long = &H80000002

'********** 注册表相关操作的 API 函数定义 **********
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, 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
Private 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 Enum RegType
   字符串 = REG_SZ
   二进制 = REG_BINARY
   数值 = REG_DWORD
End Enum

'// 注册表的根节点的值
Public Enum RegKey
   REG_USER = HKEY_CURRENT_USER
   REG_MACHINE = HKEY_LOCAL_MACHINE
End Enum

'************************************************
'** 函数名称:  GetRegSettingValue
'** 函数功能:  取得注册表的值(实际读取过程)
'** 参数说明:
'**            lngKey               注册表节点的值
'**            strItem              注册表项的名称
'** 函数返回:
'**            String 类型
'**            ""                   读取注册表失败(还有可能就是此项本身为空)
'**            注册表项的内容       读取注册表成功
'** 参考实例:
'**            strValue = GetRegSettingValue(lngKey, "值")
'************************************************
Private Function GetRegSettingValue(ByVal lngKey As Long, _
                                    ByVal strItem As String) As String
   Dim lngReturn                    As Long
   Dim uType                        As RegType
   Dim strBuffer                    As String
   Dim lngBufferSize                As Long
   Dim intValue                     As Integer
  
   '// 取得这个注册表项的类型
   lngReturn = RegQueryValueEx(lngKey, strItem, 0, uType, ByVal 0, lngBufferSize)
  
   '// 判断打开注册表项是否正确
   If lngReturn = 0 Then
      '// 判断值的类型是否为字符串
      If uType = 字符串 Then
         '// 创建一个字符串缓冲
         strBuffer = String(lngBufferSize, Chr$(0))
         '// 取得内容
         lngReturn = RegQueryValueEx(lngKey, strItem, 0, 0, ByVal strBuffer, lngBufferSize)
        
         If lngReturn = 0 Then
            '// 如果最后一个字符为Chr$(0),就删除最后一个 Chr$(0)
            If InStr(1, strBuffer, Chr$(0)) > 0 Then
               GetRegSettingValue = Left$(strBuffer, InStr(1, strBuffer, Chr$(0)) - 1)
            Else
               GetRegSettingValue = strBuffer
            End If
         End If
        
      '// 判断值的类型是否为二进制
      ElseIf uType = 二进制 Then
         '// 取得注册表项的值
         lngReturn = RegQueryValueEx(lngKey, strItem, 0, 0, intValue, lngBufferSize)
         If lngReturn = 0 Then
            GetRegSettingValue = intValue
         End If
      End If
   End If
End Function

'************************************************
'** 函数名称:  GetRegSetting
'** 函数功能:  取得注册表的值
'** 参数说明:
'**            uKey                 注册表根节点的值
'**            strPath              注册表的节点(如:Software/Microsoft/Windows/CurrentVersion/Run)
'**            strItem              注册表项的名称
'** 函数返回:
'**            String 类型
'**            ""                   读取注册表失败(还有可能就是此项本身为空)
'**            注册表项的内容       读取注册表成功
'** 参考实例:
'**            strValue = GetRegSetting(REG_MACHINE, "Software/Microsoft/Windows/CurrentVersion/Run", App.EXEName)
'************************************************
Public Function GetRegSetting(ByVal uKey As RegKey, _
                              ByVal strPath As String, _
                              ByVal strItem As String) As String
   Dim lngReturn                   As Long
  
   '// 打开注册表项
   RegOpenKey uKey, strPath, lngReturn
   '// 取得内容
   GetRegSetting = GetRegSettingValue(lngReturn, strItem)
   '// 关闭已经打开的注册表项
   RegCloseKey lngReturn
End Function

'************************************************
'** 函数名称:  DelRegSetting
'** 函数功能:  删除注册表项
'** 参数说明:
'**            uKey                 注册表根节点的值
'**            strPath              注册表的节点(如:Software/Microsoft/Windows/CurrentVersion/Run)
'**            strItem              注册表项的名称
'** 函数返回:
'**            无返回值
'** 参考实例:
'**            Call DelRegSetting(REG_MACHINE, "Software/Microsoft/Windows/CurrentVersion/Run", App.EXEName)
'************************************************
Public Sub DelRegSetting(ByVal uKey As RegKey, _
                         ByVal strPath As String, _
                         ByVal strItem As String)
   Dim lngReturn                   As Long
   '// 创建一个注册表项,如指定的项已经存在,那么函数会打开现有的项
   RegCreateKey uKey, strPath, lngReturn
   '// 删除这个项
   RegDeleteValue lngReturn, strItem
   '// 关闭
   RegCloseKey lngReturn
End Sub

'************************************************
'** 函数名称:  SaveRegSetting
'** 函数功能:  保存注册表项
'** 参数说明:
'**            uKey                 注册表根节点的值
'**            strPath              注册表的节点(如:Software/Microsoft/Windows/CurrentVersion/Run)
'**            strItem              注册表项的名称
'**            strData              注册表项的值
'**            uType                注册表项值的类型
'** 函数返回:
'**            无返回值
'** 参考实例:
'**            Call SaveRegSetting(REG_MACHINE, "Software/Microsoft/Windows/CurrentVersion/Run", App.EXEName, "我的程序目录", 字符串)
'************************************************
Public Sub SaveRegSetting(ByVal uKey As RegKey, _
                          ByVal strPath As String, _
                          ByVal strItem As String, _
                          ByVal strData As String, _
                          ByVal uType As RegType)
   Dim lngReturn                   As Long
   '// 创建一个注册表项,如指定的项已经存在,那么函数会打开现有的项
   RegCreateKey uKey, strPath, lngReturn
  
   Select Case uType
      Case 二进制
         '// 将值保存到注册表项里
         RegSetValueEx lngReturn, strItem, 0, uType, CByte(strData), 4
      Case 字符串
         '// 将值保存到注册表项里
         RegSetValueEx lngReturn, strItem, 0, uType, ByVal strData, Len(strData) + 2
   End Select
   '// 关闭注册表项
   RegCloseKey lngReturn
End Sub

阅读全文
0 0

相关文章推荐

img
取 消
img