Private Declare Function RegOpenKeyEx Lib "advapi32" 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 RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" 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
Private Declare Function RegCreateKeyEx Lib "advapi32" 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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long'系统标准常量
Const REG_SZ = 1
Const HKEY_LOCAL_MACHINE = &H80000002
Const KEY_ALL_ACCESS = &H3F
Const REG_OPTION_NON_VOLATILE = 0&
Const REG_CREATED_NEW_KEY = &H1
Const REG_OPENED_EXISTING_KEY = &H2
Const ERROR_SUCCESS = 0&'****************************************************************
'函数名: WriteReg
'说  明: 写注册表
'输  入: strName  -- 键名
'        strValue -- 键值
'返回值: 布尔值。true创建成功;反之false
'****************************************************************
Private Function WriteReg(ByRef strName As String, ByRef strValue As String) As Boolean    Dim lngReturn As Long    '返回值
    Dim lngReserved As Long  '保留值
    Dim lngPhkResult As Long '键值指针
    Dim udtTemp As SECURITY_ATTRIBUTES
    
    lngReturn = RegCreateKeyEx(HKEY_LOCAL_MACHINE, sSubkeyPath, _
        lngReserved, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _
        udtTemp, lngPhkResult, REG_CREATED_NEW_KEY)
    If lngReturn <> ERROR_SUCCESS Then GoTo lErr
    lngReturn = 0
    
    lngReturn = RegSetValueEx(lngPhkResult, strName, lngReserved, _
        REG_SZ, strValue, Len(strValue))
    If lngReturn <> ERROR_SUCCESS Then GoTo lErr
    
    lngReturn = RegCloseKey(lngPhkResult)
    
    WriteReg = True
    
    Exit Function
    
lErr:
    
End Function
'****************************************************************
'函数名: QueryReg
'说  明: 查注册表键值
'输  入: strName  -- 键名
'返回值: 键值
'****************************************************************
Private Function QueryReg(ByRef strName As String) As String    Dim lngReturn As Long    '返回值
    Dim lngReserved As Long  '保留值
    Dim lngPhkResult As Long '键值指针
    Dim strValue As String
    strValue = Space(254)
    
    lngReturn = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sSubkeyPath, _
        lngReserved, KEY_ALL_ACCESS, lngPhkResult)
    If lngReturn <> ERROR_SUCCESS Then GoTo lErr
    lngReturn = 0
    
    lngReturn = RegQueryValueEx(lngPhkResult, strName, lngReserved, _
        REG_SZ, strValue, 254)
    If lngReturn <> ERROR_SUCCESS Then GoTo lErr
    
    lngReturn = RegCloseKey(lngPhkResult)
    
    QueryReg = Trim(strValue)
    
    Dim intPst As Integer
    intPst = InStr(QueryReg, Chr(0))
    If intPst > 0 Then
        QueryReg = Left(QueryReg, intPst - 1)
    End If
    
    Exit Function
    
lErr:
        
End Function

解决方案 »

  1.   

    VB操作注册表:
    http://www.sqreg.com/file/vb/reg_01.htm
    http://www.sqreg.com/file/vb/reg_02.htm
    http://www.sqreg.com/file/vb/reg_03.htm
    http://www.sqreg.com/file/vb/reg_04.htm
    http://www.sqreg.com/file/vb/reg_05.htm
    http://www.sqreg.com/file/vb/reg_06.htm
    http://www.sqreg.com/file/vb/reg_07.htm
      

  2.   

    三楼的网址我曾经访问过,那里是不错,但我感觉那里只介绍了创建缺省键值的方法。而我需要的是创建普通键值的方法。我试着按sikeen(吸血鬼) 的方法去做,达到了一半效果,即创建出了键值名:B,但键值数据“C”在写入注册表中后变成了乱码。相应于sikeen(吸血鬼)的:
    lngReturn = RegSetValueEx(lngPhkResult, strName, lngReserved, _
            REG_SZ, strValue, Len(strValue))
    我用的实际语句是:
        lngReturn = RegSetValueEx(hKey, "B", lngReserved, REG_SZ, "C", Len("C"))
    请问这错在哪里呢?烦请各位高手指点!!!
      

  3.   

    一,为什么会错?95,98是ansi字符,nt4和2000是unicode.所以不经过转换,读写注册表肯定有问题.
    二,怎么搞定,当然写转换代码肯定可以,不过为什么要用api,有现成的为什么不用?
    fellow me
    工程|引用|registry access function打钩test!Dim oReg As New Registry
    Private Sub Form_Load()
    oReg.UpdateKey HKEY_LOCAL_MACHINE, "abc\deg", "ghi", "jkl"
    End Subbingo!
      

  4.   

    我一向是用WSH技术,简单方便。
    sub WriteReg()
    dim ws as object
    set ws=createobject("Wscript.shell")
    ws.regwrite "HKEY_LOCAL_MACHINE\A\B","C"
    end sub
      

  5.   

    是你没有仔细看http://www.sqreg.com/file/vb/reg_05.htm
      

  6.   

    RegCreateKey
    RegOpenKey
    RegSetValue