本帖最后由 fondax 于 2010-02-23 12:10:52 编辑

解决方案 »

  1.   

    第7个参数是SECURITY_ATTRIBUTES类型的,而你给的是long类型的,当然参数类型不符了
      

  2.   

    改成:Private Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
          Dim hNewKey As Long        'handle to the new key
          Dim lRetVal As Long        'result of the RegCreateKeyEx function
          Dim aa As SECURITY_ATTRIBUTES
          lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, aa, hNewKey, lRetVal)
          RegCloseKey (hNewKey)
      End Sub试试
      

  3.   

    您好BestBadGod多谢解答,按照您的代码不会出现错误提示,可是注册表并没有添加TestKey的键。SECURITY_ATTRIBUTES类型里面是3个整型,而0&好像是字符串巴?难道MSDN也会有错。另外我还忘了添加RegCloseKey的声明。(已添加完)
      

  4.   

    有两处错误:
    一、API的声明不对
    二、不能直接在HKEY_LOCAL_MACHINE下创建key
    Private Const REG_OPTION_NON_VOLATILE = 0      ' Key is preserved when system is rebooted
    Private Const KEY_ALL_ACCESS = &H3F
    Private Const HKEY_LOCAL_MACHINE = &H80000002
    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, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
    Private Type SECURITY_ATTRIBUTES
            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long
    End Type
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As LongPrivate Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
          Dim hNewKey As Long        'handle to the new key
          Dim lRetVal As Long        'result of the RegCreateKeyEx function
          lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
          RegCloseKey (hNewKey)
      End Sub
    Private Sub Command1_Click()
    CreateNewKey "software\TestKey", HKEY_LOCAL_MACHINE
    end sub
      

  5.   

    请参考Microsoft SDK\samples\multimedia\vbsamples\directshow\editing\dextervb\modregistry.bas
      

  6.   

    多谢BestBadGod,lhcwjy
    lhcwjy的答案正确40分,BestBadGod 10分
    请多包涵
    thank you everyone