改成: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试试
有两处错误: 一、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
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试试
一、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
lhcwjy的答案正确40分,BestBadGod 10分
请多包涵
thank you everyone