必须用RegCreateKeyEx!RegCreateKeyEx VB声明 
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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long 
说明 
在指定项下创建新项的更复杂的方式。在Win32环境中建议使用这个函数。如指定的项已经存在,则函数会打开现有的项 
返回值 
Long,零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码 
参数表 
参数 类型及说明 
hKey Long,一个打开项的句柄,或者一个标准项名 
lpSubKey String,欲创建的新子项的名字 
Reserved Long,设为零 
lpClass String,项的类名 
dwOptions Long,下述常数为零:REG_OPTION_VOLATILE——这个项不正式保存下来,系统重新启动后会消失 
samDesired Long,带有前缀KEY_??的一个或多个常数。它们组合起来描述了允许对这个项进行哪些操作 
lpSecurityAttributes SECURITY_ATTRIBUTES,对这个项的安全特性进行描述的一个结构(用ByVal As Long传递空值)。不适用于windows 95 
phkResult Long,指定用于装载新子项句柄的一个变量 
lpdwDisposition Long,用于装载下列某个常数的一个变量:
REG_CREATED_NEW_KEY——新建的一个子项
REG_OPENED_EXISTING_KEY——打开一个现有的项  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

解决方案 »

  1.   

    给你一个例子:
    'This program needs 3 buttons
    Const REG_SZ = 1 ' Unicode nul terminated string
    Const REG_BINARY = 3 ' Free form binary
    Const HKEY_CURRENT_USER = &H80000001
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 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
    Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
        Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
        'retrieve nformation about the key
        lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
        If lResult = 0 Then
            If lValueType = REG_SZ Then
                'Create a buffer
                strBuf = String(lDataBufSize, Chr$(0))
                'retrieve the key's content
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
                If lResult = 0 Then
                    'Remove the unnecessary chr$(0)'s
                    RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
                End If
            ElseIf lValueType = REG_BINARY Then
                Dim strData As Integer
                'retrieve the key's value
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
                If lResult = 0 Then
                    RegQueryStringValue = strData
                End If
            End If
        End If
    End Function
    Function GetString(hKey As Long, strPath As String, strValue As String)
        Dim Ret
        'Open the key
        RegOpenKey hKey, strPath, Ret
        'Get the key's content
        GetString = RegQueryStringValue(Ret, strValue)
        'Close the key
        RegCloseKey Ret
    End Function
    Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Save a string to the key
        RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
        'close the key
        RegCloseKey Ret
    End Sub
    Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Set the key's value
        RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
        'close the key
        RegCloseKey Ret
    End Sub
    Sub DelSetting(hKey As Long, strPath As String, strValue As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Delete the key's value
        RegDeleteValue Ret, strValue
        'close the key
        RegCloseKey Ret
    End Sub
    Private Sub Command1_Click()
        Dim strString As String
        'Ask for a value
        strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
        If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 Then
            MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
            Exit Sub
        End If
        'Save the value to the registry
        SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
    End Sub
    Private Sub Command2_Click()
        'Get a string from the registry
        Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
        If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
        MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
    End Sub
    Private Sub Command3_Click()
        'Delete the setting from the registry
        DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
        MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
    End Sub
    Private Sub Form_Load()
        Command1.Caption = "Set Value"
        Command2.Caption = "Get Value"
        Command3.Caption = "Delete Value"
    End Sub
      

  2.   

    这个是RegCreateKeyEx API经历和发展了很多年啦,因此,有些函数已经重复啦。比如
    RegSetValue()及  RegSetValueEx()都是用来设置注册表键值的。两者的区别
    在于前者是设置注册表键的默认值,仅支持作为数据类型的字符串,而后者不仅继
    承了前者的所有功能而且还能对多值或类型进行操作。一般API对比比较新的函数
    都回在后缀追加“Ex”的同样名称函数,建议在编程中均应尽可能的使用高级函
    数。而初级函数是向上兼容的。与其他程序相同,对注册表的编程也要用到句柄
    。通过一个句柄访问注册表键值,当打开或创建一个键值的时候,会返回一个该
    键的句柄,并且调用和分析键和创建键值,在分析和创建的同时需要传递句柄到
    函数。WINDOWS提供预定义的用语根一级键的保留句柄。
    HKEY_CLASS_ROOT,HKEY_CURRENT_USER,HKEY_LOCAL_MACHINE,HKEY_USER。这些都
    是与注册表的根键相对应并且同名的句柄。当访问一个根键的时候,传递这些通
    用句柄。这就不用打开根键啦,因为他们总是在打开状态下,可使用默认键的句
    柄访问
    Const HKEY_CURRENT_USER = &H80000001
    Const REG_OPTION_BACKUP_RESTORE = 4     ' open for backup or restore
    Const REG_OPTION_VOLATILE = 1           ' Key is not preserved when system is rebooted
    Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
    Const STANDARD_RIGHTS_ALL = &H1F0000
    Const SYNCHRONIZE = &H100000
    Const READ_CONTROL = &H20000
    Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Const KEY_CREATE_LINK = &H20
    Const KEY_CREATE_SUB_KEY = &H4
    Const KEY_ENUMERATE_SUB_KEYS = &H8
    Const KEY_NOTIFY = &H10
    Const KEY_QUERY_VALUE = &H1
    Const KEY_SET_VALUE = &H2
    Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    Const KEY_EXECUTE = (KEY_READ)
    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 Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    Private 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, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Sub Form_Load()
        Dim Result As Long
        'Check if the specified key exists
        RegOpenKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, KEY_ALL_ACCESS, Result
        'If the key doesn't exist, we create it
        If Result = 0 Then
            'Create a new key
            RegCreateKeyEx HKEY_CURRENT_USER, "KPD-Team", 0, "REG_DWORD", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, Result, Ret
            If Result = 0 Then
                MsgBox "Error while creating the Key!!"
                Exit Sub
            End If
        End If
        'Delete the key
        RegDeleteKey Result, ""
        'close the handle
        RegCloseKey Result
    End Sub