一、常数定义和API申明(我们会在使用该API时对它进行说明): Private Const HKEY_CLASSES_ROOT = &H80000000 主键 Any, lpcbData As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long 二、将下列代码放在窗体的Load事件中 return_OpenKey =RegOpenKeyEx(HKEY_CURRENT_USER, "",0, KEY_ALL_ACCESS, hKey) 下面的函数RegSetValueEx给指定的键值名称赋键值 If RegSetValueEx(hKey, KeyValue1, 0&, REG_SZ, ByVal KeyData, Len(KeyData) + 1) <> ERROR_SUCCESS Then 下面的函数RegSetValueEx给指定的键值名称"学生" 赋键值为"15020" If RegSetValueEx(hKey, KeyValue2, 0&, REG_SZ, ByVal KeyData, Len(KeyData) + 1) <> ERROR_SUCCESS Then 下面的函数RegQueryValueEx检索指定键值名称的键值和键值类型 If RegQueryValueEx(hKey, KeyValue1, 0&, REG_SZ, ByVal lpData, SizeOfData) <> ERROR_SUCCESS Then 运行此程序后,打开注册表,我们会发现在主键 HKEY_CURRENT_USER(在此主键下记录本机当前用户的一些信息)下,增加了子键"北信BITI\People",我们还可看到两个指定的键值名称都有正确的键值。从而完成了注册的任务。
Windows系统中的注册表(Resgister)保存着大量的信息,我们可读写注册表来更好地为我们软件服务。
下面详细讲叙如何通过调用API函数操作注册表:如何打开、创建主键,如何保存键值,以及查询某键的键值等。
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
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
下面的函数RegCreateKey建立一个主键,如此键已存在,则打开它 If RegCreateKey(hKey, PriKey, hKey) <> ERROR_SUCCESS Then
MsgBox "create " & PriKey & "Failed"
Exit Sub
End If
Dim KeyData As String 键值
KeyData = "500"
MsgBox "SetValue " & KeyValue1 & "Failed "
Exit Sub
End If
KeyData = "15020"
MsgBox "SetValue " & KeyValue2 & "Failed "
Exit Sub
End If
return_OpenKey = RegQueryValueEx(hKey, KeyValue1, 0&, REG_SZ, 0&, SizeOfData)
lpData = String(SizeOfData + 1, " ")
MsgBox "Query " & KeyValue1 & "Failed"
End If
RegCloseKey (hKey) 释放主键的句柄
End Sub
文章来源于领测软件测试网 https://www.ltesting.net/
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073