'操作注册彪函数Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&' Registry API prototypesDeclare 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 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 RegOpenKey Lib "advapi32.DLL" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 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
Public Const REG_SZ = 1                         ' Unicode nul terminated string
Public Const REG_DWORD = 4                      ' 32-bit numberPublic Sub savekey(hKey As Long, strPath As String)
Dim keyhand&
r = RegCreateKey(hKey, strPath, keyhand&)
r = RegCloseKey(keyhand&)
End SubPublic Function getstring(hKey As Long, strPath As String, strValue As String)Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(hKey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
    strBuf = String(lDataBufSize, " ")
    lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
    If lResult = ERROR_SUCCESS Then
        intZeroPos = InStr(strBuf, Chr$(0))
        If intZeroPos > 0 Then
            getstring = Left$(strBuf, intZeroPos - 1)
        Else
            getstring = strBuf
        End If
    End If
End If
End Function
Public Sub savestring(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, reglen)
'r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))r = RegCloseKey(keyhand)
End Sub
Function getdword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Longr = RegOpenKey(hKey, strPath, keyhand) ' Get length/data type
lDataBufSize = 4
    
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)If lResult = ERROR_SUCCESS Then
    If lValueType = REG_DWORD Then
        getdword = lBuf
    End If
'Else
'    Call errlog("GetDWORD-" & strPath, False)
End Ifr = RegCloseKey(keyhand)
    
End FunctionFunction SaveDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
    Dim lResult As Long
    Dim keyhand As Long
    Dim r As Long
    r = RegCreateKey(hKey, strPath, keyhand)
    lResult = RegSetValueEx(keyhand, strValueName, 0&, REG_DWORD, lData, 4)
    'If lResult <> error_success Then Call errlog("SetDWORD", False)
    r = RegCloseKey(keyhand)
End FunctionPublic Function DeleteKey(ByVal hKey As Long, ByVal strKey As String)
Dim r As Long
r = RegDeleteKey(hKey, strKey)
End FunctionPublic Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
r = RegOpenKey(hKey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)
End Function'调用
Call savestring(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\explorer\Advanced\Folder", "a\b", 2)Call SaveDword ……写2进制健getdword 得到2进制健getstring得到字符串健

解决方案 »

  1.   

    读 / 写任何注册表键值
    ' module declarationsPublic Const READ_CONTROL = &H20000
    Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Public Const KEY_QUERY_VALUE = &H1
    Public Const KEY_SET_VALUE = &H2
    Public Const KEY_CREATE_SUB_KEY = &H4
    Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    Public Const KEY_NOTIFY = &H10
    Public Const KEY_CREATE_LINK = &H20
    Public Const SYNCHRONIZE = &H100000
    Public Const STANDARD_RIGHTS_ALL = &H1F0000
    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_WRITE = ((STANDARD_RIGHTS_WRITE Or _
       KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    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))
    Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))Public Const ERROR_SUCCESS = 0&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 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 RegCloseKey Lib "advapi32.dll" _
       (ByVal hKey As Long) As Long
    Function sdaGetRegEntry(strKey As String, _
       strSubKeys As String, strValName As String, _
       lngType As Long) As String
    '  Demonstration of win32 API's to query
    '  the system registry
    On Error GoTo sdaGetRegEntry_Err  Dim lngResult As Long, lngKey As Long
      Dim lngHandle As Long, lngcbData As Long
      Dim strRet As String  Select Case strKey
        Case "HKEY_CLASSES_ROOT": lngKey = &H80000000
        Case "HKEY_CURRENT_CONFIG": lngKey = &H80000005
        Case "HKEY_CURRENT_USER": lngKey = &H80000001
        Case "HKEY_DYN_DATA": lngKey = &H80000006
        Case "HKEY_LOCAL_MACHINE": lngKey = &H80000002
        Case "HKEY_PERFORMANCE_DATA": lngKey = &H80000004
        Case "HKEY_USERS": lngKey = &H80000003
        Case Else: Exit Function
      End Select
        
      If Not ERROR_SUCCESS = RegOpenKeyEx(lngKey, _
         strSubKeys, 0&, KEY_READ, _
         lngHandle) Then Exit Function
      
      lngResult = RegQueryValueEx(lngHandle, strValName, _
         0&, lngType, ByVal strRet, lngcbData)
      strRet = Space(lngcbData)
      lngResult = RegQueryValueEx(lngHandle, strValName, _
         0&, lngType, ByVal strRet, lngcbData)
      
      If Not ERROR_SUCCESS = RegCloseKey(lngHandle) Then _
         lngType = -1&
        
      sdaGetRegEntry = strRet
      
    sdaGetRegEntry_Exit:
      On Error GoTo 0
      Exit FunctionsdaGetRegEntry_Err:
      lngType = -1&
      MsgBox Err & ">  " & Error$, 16, _
         "GenUtils/sdaGetRegEntry"
      Resume sdaGetRegEntry_ExitEnd Function
      Dim lngType As Long, varRetString As Variant
      Dim lngI As Long, intChar As Integer  varRetString = sdaGetRegEntry(cboStartKey, _
       txtRegistrationPath, txtRegistrationParameter, _
       lngType)
      
      txtResult = varRetString
      txtDataType = lngType
      txtDataLength = Len(varRetString)
        
      txtHex = ""
      If Len(varRetString) Then
        For lngI = 1 To Len(varRetString)
          intChar = Asc(Mid(varRetString, lngI, 1))
          If intChar > 15 Then
            txtHex = txtHex & Hex(intChar) & " "
          Else
            txtHex = txtHex & "0" & Hex(intChar) & " "
          End If
        Next lngI
      End If