教你在VB中如何使用注册表
2008-02-23 06:50:02来源:互联网 阅读 ()
下面详细讲叙如何通过调用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中运行通过。
上一篇: VB查找并替换WORD文档中的字符串
下一篇: VB编程实用精典小技巧3例
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:VB与大型数据库无数据源的连接
下一篇:教你认识VB的座标系统
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash