• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

教你在VB中如何使用注册表

发布: 2007-6-21 21:57 | 作者:   | 来源:   | 查看: 24次 | 进入软件测试论坛讨论

领测软件测试网

   
  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",我们还可看到两个指定的键值名称都有正确的键值。从而完成了注册的任务。

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网