首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

天极网 > 软件频道 > 设计在线 > 网页UI设计 >用VB5读写注册表实例

用VB5读写注册表实例

2000-09-05 00:00作者:-出处:VB编程乐园责任编辑:

 ∈紫刃陆ㄒ桓龉こ蹋谛陆ǖ拇疤迳咸砑右韵驴丶⑸柚孟嘤κ粜裕?
控件名 属性 值
Label1 Caption 注册表键值:
Label2 Caption 注册姓名:
Label3 Caption 空
Label4 Caption 注册公司:
Label5 Caption 空
Label6 Caption 空
Text1 Text 空
Text2 Text 空
Command1 Caption 写入注册表
Command2 Caption 退出
  然后,在程序的声明部分添加如下代码:
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 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
Const error_success = 0&
Const error_baddb = 1009&
Const error_badkey = 1010&
Const error_cantopen = 1011&
Const error_cantread = 1012&
Const error_cantwrite = 1013&
Const error_registry_recovered = 1014&
Const error_registry_corrupt = 1015&
Const error_registry_io_failed = 1016&
Const hkey_classes_root = &H80000000
Const hkey_current_user = &H80000001
Const hkey_local_machine = &H80000002
Const REG_SZ = 1
Const regkey = "software\My Soft\My program"
  接着为各个控件添加代码,代码如下:
Private Sub Command1_Click()
Dim keyvalue As String
Dim retvalue As Long
Dim keyid As Long
retvalue = RegCreateKey(hkey_local_machine, regkey, keyid)
keyvalue = Text1.Text
retvalue = RegSetValueEx(keyid, "注册名", 0&, REG_SZ, ByVal keyvalue, Len(keyvalue) + 1)
keyvalue = Text2.Text
retvalue = RegSetValueEx(keyid, "注册公司", 0&, REG_SZ, ByVal keyvalue, Len(keyvalue) + 1)
If Text1.Text $#@60;$#@62; "" And Text2. t $#@60;$#@62; "" Then
Label3.Caption = Text1.Text
Label5.Caption = Text2.Text
End If
End Sub

Private Sub Command2_Click()
Unload Me
End
End Sub

Private Sub Form_Load()
Dim retvalue As Long
Dim result As Long
Dim keyid As Long
Dim keyvalue As String
Dim subkey As String
Dim bufsize As Long
Label6.Caption = regkey
retvalue = RegCreateKey(hkey_local_machine, regkey, keyid)
If retvalue = 0 Then
subkey = "注册名"
retvalue = RegQueryValueEx(keyid, subkey, 0&, REG_SZ, 0&, bufsize)
If bufsize $#@60; 2 Then
keyvalue = ""
retvalue = RegSetValueEx(keyid, subkey, 0&, REG_SZ, ByVal keyvalue, Len(keyvalue) + 1)
Else
keyvalue = String(bufsize + 1, " ")
retvalue = RegQueryValueEx(keyid, subkey, 0&, REG_SZ, ByVal keyvalue, bufsize)
keyvalue = Left$(keyvalue, bufsize - 1)
Text1.Text = keyvalue
End If
Label3.Caption = keyvalue

subkey = "注册公司"
retvalue = RegQueryValueEx(keyid, subkey, 0&, REG_SZ, 0&, bufsize)
If bufsize $#@60; 2 Then
keyvalue = ""
retvalue = RegSetValueEx(keyid, subkey, 0&, REG_SZ, ByVal keyvalue, Len(keyvalue) + 1)
Else
keyvalue = String(bufsize + 1, " ")
retvalue = RegQueryValueEx(keyid, subkey, 0&, REG_SZ, ByVal keyvalue, bufsize)
keyvalue = Left$(keyvalue, bufsize - 1)
Text2.Text = keyvalue
End If
Label5.Caption = keyvalue
End If
End Sub
  好了,存盘,按F5运行程序,在两个Text中分别写入你的“注册姓名”和“注册公司”的名称,然后单击“写入注册表”按钮,现在打开“注册表编辑器”,找到HKEY_LOCAL_MACHINE\SOFTWARE\My Soft\My Program,看看右边是不是生成了“注册名”和“注册公司”两个键值,是的话,恭喜你,你成功了。

关注此文的读者还看过:

返回软件频道首页

软件频道最新更新

热点推荐

IT嘉年华

编辑推荐

热门
推荐

网友关注

软件
资料
游戏

文章排行

本周
本月
最新更新
天极服务| 关于我们| About us| 网站律师| 电子杂志| RSS订阅| 友情合作| 加入我们| 网站地图| MSN/QQ上看天极
TMG
Copyright (C) 1999-2009 Yesky.com, All Rights Reserved
版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868 在线提交意见反馈
渝ICP证B2-20030003号  通用网址:天极网
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众
经营性网站备案信息 网警备案 中国网站排名