2.
HOWTO: Enumerate the Subkeys of an Open Registry Key
http://support.microsoft.com/support/kb/articles/q267/9/08.asp?LN=EN-US&SD=gn&FR=0&qry=&rnk=6&src=DHCS_MSPSS_gn_SRCH&SPR=VBB

解决方案 »

  1.   

    1.Start a new project Standard EXE in Visual Basic. Form1 is created by default.
    2.Set the BorderStyle property of Form1 to 4 - FixedToolWindow.
    3.Add the following controls to Form1: three Labels, two ComboBoxs, one TextBox, two CommandButtons, one CheckBox, and one ListBox.
    4.Set the Style property for each ComboBox to 2 - Dropdown List.
    5.Add the following code to the General Declarations section of Form1:
    Option ExplicitPrivate Type FILETIME
        intLow As Long
        intHigh As Long
    End TypePrivate 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 LongPrivate 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 LongPrivate Declare Function RegCloseKey Lib "advapi32.dll" _
        (ByVal hKey As Long) As LongConst HKEY_CLASSES_ROOT = &H80000000
    Const HKEY_CURRENT_USER = &H80000001
    Const HKEY_LOCAL_MACHINE = &H80000002
    Const HKEY_USERS = &H80000003Const 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 LongPrivate 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 SubPrivate 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 SubPrivate Sub Command2_Click()
        Unload Me
    End SubPrivate 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.Run the project and execute a search. Note that Keys that match the search criteria are displayed in the ListBox.
      

  2.   

    Private Sub Command1_Click()
        '利用注册表操作API查询当前的MCI设备名称
        Dim astr As String
        Dim l As Long
        
        For l = 0 To 15
            If EnumKey(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\run", l, astr) Then
                List1.AddItem astr
            End If
        Next
    End Sub'一下保存在体格模块利
    Option ExplicitType ACL
            AclRevision As Byte
            Sbz1 As Byte
            AclSize As Integer
            AceCount As Integer
            Sbz2 As Integer
    End TypeType SECURITY_DESCRIPTOR
            Revision As Byte
            Sbz1 As Byte
            Control As Long
            Owner As Long
            Group As Long
            Sacl As ACL
            Dacl As ACL
    End TypeType SECURITY_ATTRIBUTES
            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long
    End TypeType FILETIME
            dwLowDateTime As Long
            dwHighDateTime As Long
    End TypePublic Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_CONFIG = &H80000005
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_DYN_DATA = &H80000006
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_PERFORMANCE_DATA = &H80000004
    Public Const HKEY_USERS = &H80000003Public Const ERROR_SUCCESS = 0&
    Global Const ERROR_NONE = 0
    Global Const ERROR_BADDB = 1
    Global Const ERROR_BADKEY = 2
    Global Const ERROR_CANTOPEN = 3
    Global Const ERROR_CANTREAD = 4
    Global Const ERROR_CANTWRITE = 5
    Global Const ERROR_OUTOFMEMORY = 6
    Global Const ERROR_INVALID_PARAMETER = 7
    Global Const ERROR_ACCESS_DENIED = 8
    Global Const ERROR_INVALID_PARAMETERS = 87
    Global Const ERROR_NO_MORE_ITEMS = 259Public Const REG_BINARY = 3
    Public Const REG_CREATED_NEW_KEY = &H1
    Public Const REG_DWORD = 4
    Public Const REG_DWORD_BIG_ENDIAN = 5
    Public Const REG_DWORD_LITTLE_ENDIAN = 4
    Public Const REG_EXPAND_SZ = 2
    Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9
    Public Const REG_LINK = 6
    Public Const REG_MULTI_SZ = 7
    Public Const REG_NONE = 0
    Public Const REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
    Public Const REG_NOTIFY_CHANGE_LAST_SET = &H4
    Public Const REG_NOTIFY_CHANGE_NAME = &H1
    Public Const REG_NOTIFY_CHANGE_SECURITY = &H8
    Public Const REG_OPENED_EXISTING_KEY = &H2
    Public Const REG_OPTION_BACKUP_RESTORE = 4
    Public Const REG_OPTION_CREATE_LINK = 2
    Public Const REG_OPTION_NON_VOLATILE = 0
    Public Const REG_OPTION_RESERVED = 0
    Public Const REG_OPTION_VOLATILE = 1
    Public Const REG_REFRESH_HIVE = &H2
    Public Const REG_RESOURCE_LIST = 8
    Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
    Public Const REG_SZ = 1
    Public Const REG_WHOLE_HIVE_VOLATILE = &H1
    Public Const REG_LEGAL_CHANGE_FILTER = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
    Public Const REG_LEGAL_OPTION = (REG_OPTION_RESERVED Or REG_OPTION_NON_VOLATILE Or REG_OPTION_VOLATILE Or REG_OPTION_CREATE_LINK Or REG_OPTION_BACKUP_RESTORE)Public Const READ_CONTROL = &H20000
    Public Const STANDARD_RIGHTS_ALL = &H1F0000
    Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Public Const SYNCHRONIZE = &H100000
    Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)Public Const KEY_CREATE_LINK = &H20
    Public Const KEY_CREATE_SUB_KEY = &H4
    Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    Public Const KEY_EVENT = &H1Public Const KEY_NOTIFY = &H10
    Public Const KEY_QUERY_VALUE = &H1
    Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Public Const KEY_SET_VALUE = &H2
    Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))Public Const KEY_EXECUTE = (KEY_READ)
    Public 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))Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
    Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
    Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
    Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
    Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Declare Function RegGetKeySecurity Lib "advapi32.dll" (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR, lpcbSecurityDescriptor As Long) As Long
    Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
    Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" (ByVal hKey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal hEvent As Long, ByVal fAsynchronus As Long) As Long
    Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    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
    Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
    Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
    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
    Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long
    Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
    Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
    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
    Declare Function RegUnLoadKey Lib "advapi32.dll" Alias "RegUnLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As LongDeclare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
    Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
    Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
    Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
    Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As LongPublic Function EnumKey(hMainKey As Long, sSubKey As String, lIndex As Long, lpStr As String) As Boolean
    'EnumKey函数打开有hMainKey主键和sSubKey子键指定的注册键,lIndex为要查询的子键值
    '的索引,lpStr为放置子键值的字符串缓冲,如果要查询一个键值的所有子键,只要将lIndex
    '首先设置为0,然后将lIndex递增1再调用EnumKey函数,直到函数返回0为止
        Dim hKey As Long    '打开键的句柄
        Dim i As Long
        
        If RegOpenKey(hMainKey, sSubKey, hKey) = ERROR_SUCCESS Then
            lpStr = Space(255) + Chr(0)
            Debug.Print Len(lpStr)
            If RegEnumKey(hKey, lIndex, lpStr, Len(lpStr)) = ERROR_SUCCESS Then
                EnumKey = True
            Else
                EnumKey = False
            End If
        Else
            EnumKey = False
        End If
        RegCloseKey hKey
    End Function
    Public Function DeleteKey(lPredefinedKey As Long, sKeyName As String)
    'DeleteKey函数打开有hPredfineKeyKey主键和sKeyName子键指定的注册键,再将此子键删除
        Dim lRetVal As Long
        Dim hKey As Long         '打开键的句柄
            
        lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
        lRetVal = RegDeleteKey(lPredefinedKey, sKeyName)
        RegCloseKey (hKey)
    End FunctionPublic Function DeleteValue(lPredefinedKey As Long, sKeyName As String, sValueName As String)
    'DeleteValue函数删除一个值
        Dim lRetVal As Long
        Dim hKey As Long     '打开键的句柄
        
        lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
        lRetVal = RegDeleteValue(hKey, sValueName)
        RegCloseKey (hKey)
    End FunctionPublic Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
    'SetValueEx函数设置值
    '如果参数为REG_SZ则设置的值为字符串
    '如果参数为REG_WORD设置的值为整数值
        Dim lValue As Long
        Dim sValue As String    Select Case lType
            Case REG_SZ
                sValue = vValue
                SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue))
            Case REG_DWORD
                lValue = vValue
                SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
            End Select
    End FunctionFunction QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
        Dim cch As Long
        Dim lrc As Long
        Dim lType As Long
        Dim lValue As Long
        Dim sValue As String    On Error GoTo QueryValueExError    lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
        If lrc <> ERROR_NONE Then Error 5    Select Case lType
            '查询字符串值
            Case REG_SZ:
                sValue = String(cch, 0)
                lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
                If lrc = ERROR_NONE Then
                    vValue = Left$(sValue, cch)
                Else
                    vValue = Empty
                End If        '查询整数值
            Case REG_DWORD:
                lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
                If lrc = ERROR_NONE Then vValue = lValue
            Case Else
                lrc = -1
        End SelectQueryValueExExit:    QueryValueEx = lrc
        Exit FunctionQueryValueExError:    Resume QueryValueExExitEnd Function
    Public Function CreateNewKey(lPredefinedKey As Long, sNewKeyName As String)
    ' Description:
    ' 这个函数建立一个新的键
        Dim hNewKey As Long         '打开新键的句柄
        Dim lRetVal As Long
        
        lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
        RegCloseKey (hNewKey)
    End FunctionPublic Function SetKeyValue(lPredefinedKey As Long, sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long)
           Dim lRetVal As Long
           Dim hKey As Long         '打开键的句柄       lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
           lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
           RegCloseKey (hKey)
    End FunctionPublic Function QueryValue(lPredefinedKey As Long, sKeyName As String, sValueName As String)
           Dim lRetVal As Long
           Dim hKey As Long         '打开键的句柄
           Dim vValue As Variant      'setting of queried value
           lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
           lRetVal = QueryValueEx(hKey, sValueName, vValue)
           QueryValue = vValue
           RegCloseKey (hKey)
    End Function