我的VB作品|初学指南|编程技巧|源码下载|工具控件|VBA应用|Blog|【电信线路】|【网通线路】
   位置: VB知识库 >> 编程技巧 >> 注册表 >> 正文
最新调查
    你是怎样知道VB知识库的?
通过搜索引擎
即时通讯工具
朋友介绍
友情链接
其它

  

频道统计
如何枚举注册表打开项的所有子项[VB知识库-Visual Basic Knowledge base]
如何枚举注册表打开项的所有子项[VB知识库-Visual Basic Knowledge base]
 更新时间:2008-2-29 19:26:24  点击数:11
【字体: 字体颜色

介绍如何使用 API 函数来查找键编程系统注册表中的示例。 该示例说明搜索的根子项对指定文本字符串。 显示选项来执行区分大小写文本搜索。

示例使用 RegEnumKeyEx API 函数来枚举子项的指定打开注册表项。 函数检索一个子项每次调用该信息。

以下步骤创建, 在列表框中显示子项打开注册表项的项目。 使用搜索条件以隔离特定键被返回。

1. VisualBasic 中启动新的 标准 EXE 项目。 默认情况下创建 Form 1。
2. Form 1 的 BorderStyle 属性设置为 4 - FixedToolWindow 。
3. 将以下控件添加到 Form 1: 标签 三、 ComboBoxs 两、 一个 TextBox 、 CommandButtons 两、 一个 CheckBox 和一个 ListBox 。
4. 对于每 ComboBox 为 2时 - 下拉列表 设置 样式 属性。
5. 将以下代码添加到 Form 1 的常规声明部分:
Option Explicit

Private Type FILETIME
    intLow As Long
    intHigh As Long
End Type

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 RegEnumKeyEx Lib "advapi32.dll" _
    Alias "RegEnumKeyExA" _
    (ByVal hKey As Long, _
    ByVal dwIndex As Long, _
    ByVal lpName As String, _
    lpcbName As Long, _
    ByVal lpReserved As Long, _
    ByVal lpClass As String, _
    lpcbClass As Long, _
    lpftLastWriteTime As FILETIME) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long

Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

Const ERROR_SUCCESS = 0&

Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_READ = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
                  KEY_QUERY_VALUE Or _
                  KEY_ENUMERATE_SUB_KEYS Or _
                  KEY_NOTIFY) And _
                  (Not SYNCHRONIZE))

Dim strBranch As Long

Private Sub Combo1_Click()
    ' Set the branch to search depending on
    ' what is selected in the ComboBox
    Select Case Combo1.ListIndex
        Case 0
            strBranch = HKEY_CLASSES_ROOT
        Case 1
            strBranch = HKEY_CURRENT_USER
        Case 2
            strBranch = HKEY_LOCAL_MACHINE
        Case Else
            strBranch = HKEY_USERS
    End Select
End Sub

Private Sub Command1_Click()
    Dim i As Integer
    Dim lngKeyHandle As Long
    Dim lngResult As Long
    Dim lngCurIdx As Long
    Dim strValue As String
    Dim lngValueLen As Long
    Dim strClass As String
    Dim lngClassLen As Long
    Dim strResult As String
    Dim lngTime As FILETIME
    Dim strSearch As String
    Dim intSearchLen As Integer
    Dim blnMatch As Boolean
    
    i = 0
    ' Clear the current results
    List1.Clear
    ' Assign the new string to search for
    strSearch = Text1.Text
    intSearchLen = Len(strSearch)
    
    ' Open the Root Branch to search
    lngResult = RegOpenKeyEx(strBranch, _
            "", _
             0&, _
             KEY_READ, _
             lngKeyHandle)
    
    If lngResult <> ERROR_SUCCESS Then
        MsgBox "Cannot open key.", , "Search Registry Keys"
    Else
    ' If the Root branch can be opened, disable
    ' the buttons and begin the search
        Command1.Enabled = False
        Command2.Enabled = False
        List1.Enabled = False
        Form1.MousePointer = 11
        
        lngCurIdx = 0
        Do
            lngValueLen = 2000
            strValue = String(lngValueLen, 0)
            lngClassLen = 2000
            strClass = String(lngClassLen, 0)
        
            ' Enumerate all the sub keys
            lngResult = RegEnumKeyEx(lngKeyHandle, _
                 lngCurIdx, _
                 ByVal strValue, _
                 lngValueLen, _
                 0&, _
                 ByVal strClass, _
                 lngClassLen, _
                 lngTime)
           
            ' Increment the index of found keys
            lngCurIdx = lngCurIdx + 1
        
            If lngResult = ERROR_SUCCESS Then
                ' Trim the current key to its actual length
                strResult = Left(strValue, lngValueLen)
                
                ' Eliminate case if the search is insensitive
                blnMatch = False
                strValue = strResult
                If Check1.Value = 0 Then
                    strResult = LCase(strResult)
                    strSearch = LCase(strSearch)
                End If

                ' Compare strings based upon search type
                Select Case Combo2.ListIndex
                    Case 0
                        ' Check if any portion of the search string is found.
                        If InStr(strResult, strSearch) Then blnMatch = True
                    Case 1
                        ' Check if an exact match is found.
                        If strResult = strSearch Then blnMatch = True
                    Case 2
                        ' Check if the search string matches the
                        ' left portion of the key string.
                        If Left(strResult, intSearchLen) = strSearch Then blnMatch = True
                    Case Else
                        ' Check if the search string matches the 
                        ' right portion of the key string.
                        If Right(strResult, intSearchLen) = strSearch Then blnMatch = True
                End Select
                
                ' Populate the list with keys that match
                ' the search criteria
                If blnMatch Then
                    i = i + 1
                    List1.AddItem strValue
                End If
            End If
        
        ' Keep looking for more keys
        Loop While lngResult = ERROR_SUCCESS
        ' Close the Root Branch
        lngResult = RegCloseKey(lngKeyHandle)
    
        ' Enable the buttons
        Form1.MousePointer = 0
        List1.Enabled = True
        Command1.Enabled = True
        Command2.Enabled = True
        
        ' Display the total matches
        MsgBox "Total matches:" & Str(i), , "Search Registry Keys"
    End If
End Sub

Private Sub Command2_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    ' Set up the Form interface
    Form1.Caption = "Search Registry Keys"
    Form1.Move 0, 0, 6960, 4230
    
    Label1.Caption = "Search Branch:"
    Label1.Move 240, 240, 1215, 255
    
    Label2.Caption = "Search Mode:"
    Label2.Move 240, 720, 1215, 255
    
    Label3.Caption = "Find What:"
    Label3.Move 240, 1200, 1215, 255

    Combo1.Move 1680, 240, 2535
    Combo1.AddItem "HKEY_CLASSES_ROOT"
    Combo1.AddItem "HKEY_CURRENT_USER"
    Combo1.AddItem "HKEY_LOCAL_MACHINE"
    Combo1.AddItem "HKEY_USERS"
    Combo1.ListIndex = 0
    Combo1.TabIndex = 0
    
    Combo2.Move 1680, 720, 2535
    Combo2.AddItem "Portion"
    Combo2.AddItem "All"
    Combo2.AddItem "Left"
    Combo2.AddItem "Right"
    Combo2.ListIndex = 0
    Combo2.TabIndex = 1
    
    Text1.Move 1680, 1200, 2535, 350
    Text1.Text = ""
    Text1.TabIndex = 2
    
    Command1.Caption = "Find Keys"
    Command1.Move 4560, 240, 1935, 375
    Command1.TabIndex = 3
    Command1.Default = True
    
    Command2.Caption = "Close"
    Command2.Move 4560, 720, 1935, 375
    Command2.TabIndex = 4
    
    Check1.Caption = "Match Case"
    Check1.Move 4680, 1320, 1275, 255
    Check1.TabIndex = 5

    List1.Move 240, 1920, 6255, 1620
    List1.TabIndex = 6
End Sub
					
6. 运行该项目并执行搜索。 注意, 将显示在 ListBox 键符合搜索条件。


至少要成为本站的注册会员才能下载! 注册点我!
  • 上一篇: 用VB操作注册表
  • 下一篇: VB自带注册表函数示例之SaveSetting
  • 发表评论   告诉好友   打印此文  收藏此页  关闭窗口  返回顶部
    热点文章
     
    推荐文章
     
    相关文章
    网友评论:(只显示最新5条。)
    河北宝宝网河北教育网址导航VB知识库点击申请点击申请点击申请点击申请点击申请点击申请点击申请
    点击申请点击申请点击申请点击申请点击申请点击申请
    Copyright© 2008 vbkbase.com All Rights Reserved QQ群:54150844
    冀ICP备08000517号