VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 在VB中使用注册表
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:12024, 日期:2002-06-04
  Windows系统中的注册表(Resgister)保存着大量的信息,我们可读写注册表来更好地为我们软件服务。

  下面详细讲叙如何通过调用API函数操作注册表:如何打开、创建主键,如何保存键值,以及查询某键的键值等。

  一、常数定义和API申明(我们会在使用该API时对它进行说明):   Private Const HKEY_CLASSES_ROOT = &H80000000 主键
  Private Const HKEY_CURRENT_USER = &H80000001 主键
  Private Const STANDARD_RIGHTS_ALL = &H1F0000
  Private Const KEY_ENUMERATE_SUB_KEYS = &H8
  Private Const KEY_SET_VALUE = &H2
  Private Const KEY_CREATE_SUB_KEY = &H4
  Private Const KEY_QUERY_VALUE = &H1
  Private Const KEY_NOTIFY = &H10
  Private Const KEY_CREATE_LINK = &H20
  Private Const SYNCHRONIZE = &H100000
  Private 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))
  Private Const REG_MULTI_SZ = 7
  Private Const ERROR_SUCCESS = 0&
  Private Const READ_CONTROL = &H20000
  Private Const REG_SZ = 1
  Private Const REG_DWORD = 4
  Private Const REG_EXPAND_SZ = 2
  Private Const REG_BINARY = 3
  Private Const REG_DWORD_BIG_ENDIAN = 5
  Private Const REG_DWORD_LITTLE_ENDIAN = 4
  Private Const REG_NONE = 0
  Private 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
  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 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
  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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long


  二、将下列代码放在窗体的Load事件中
  Private Sub Form_Load()
  Dim hKey As Long 主键的句柄
  Dim lpData As String 查询得到的键值
  Dim SizeOfData As Long 键值的长度
  Dim ValueType As Long 键值的类型
  SizeOfData = 150
  Dim return_OpenKey As Long 函数调用的返回值
  Dim PriKey As String 主键
  Dim KeyValue1, KeyValue2 As String 键值名称
  PriKey = "北信BITI\People" 主键
  KeyValue1 = "教授" 键值名称
  KeyValue2 = "学生" 键值名称
  下面的函数RegOpenKeyEx取得一个句柄hKey

return_OpenKey =RegOpenKeyEx(HKEY_CURRENT_USER, "",0, KEY_ALL_ACCESS, hKey)
 
  下面的函数RegCreateKey建立一个主键,如此键已存在,则打开它 If RegCreateKey(hKey, PriKey, hKey) <> ERROR_SUCCESS Then
MsgBox "create " & PriKey & "Failed"
Exit Sub
End If
  
Dim KeyData As String 键值
KeyData = "500"

  下面的函数RegSetValueEx给指定的键值名称赋键值

If RegSetValueEx(hKey, KeyValue1, 0&, REG_SZ, ByVal KeyData, Len(KeyData) + 1) <> ERROR_SUCCESS Then
   MsgBox "SetValue " & KeyValue1 & "Failed "
   Exit Sub
   End If
   KeyData = "15020"

  下面的函数RegSetValueEx给指定的键值名称"学生" 赋键值为"15020"

If RegSetValueEx(hKey, KeyValue2, 0&, REG_SZ, ByVal KeyData, Len(KeyData) + 1) <> ERROR_SUCCESS Then
MsgBox "SetValue " & KeyValue2 & "Failed "
Exit Sub
End If
  
return_OpenKey = RegQueryValueEx(hKey, KeyValue1, 0&, REG_SZ, 0&, SizeOfData)
lpData = String(SizeOfData + 1, " ")

  下面的函数RegQueryValueEx检索指定键值名称的键值和键值类型

If RegQueryValueEx(hKey, KeyValue1, 0&, REG_SZ, ByVal lpData, SizeOfData) <> ERROR_SUCCESS Then
MsgBox "Query " & KeyValue1 & "Failed"
End If
RegCloseKey (hKey) 释放主键的句柄
End Sub

  运行此程序后,打开注册表,我们会发现在主键 HKEY_CURRENT_USER(在此主键下记录本机当前用户的一些信息)下,增加了子键"北信BITI\People",我们还可看到两个指定的键值名称都有正确的键值。从而完成了注册的任务。

  注:此程序在VB5.0中运行通过。