RegCreateKey可以建立subkey,怎么建立呢?

hongren88888 2009-02-26 12:53:01
RegCreateKey可以建立subkey,怎么建立呢?
...全文
334 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
bob008 2009-03-05
  • 打赏
  • 举报
回复
都搜集了好多啊
嗷嗷叫的老马 2009-03-02
  • 打赏
  • 举报
回复
http://www.m5home.com/bbs/dispbbs.asp?boardid=28&Id=422&page=2

来来,封装好的一个读写模块......
imkeby 2009-03-01
  • 打赏
  • 举报
回复


Private Function SetValueEx(ByVal lngHwdKey As Long, ByVal strValueName _
As String, ByVal lngType As Long, ByVal varValueData As Variant) _
As Long

'Declare variables
Dim lngValue As Long
Dim strValue As String

'Determine the size and type of data to be written
Select Case lngType

'Strings
Case REG_SZ
strValue = varValueData & Chr$(0)
SetValueEx = RegSetValueExString(lngHwdKey, strValueName, _
0&, lngType, strValue, Len(strValue))

'DWORDs
Case REG_DWORD
lngValue = varValueData
SetValueEx = RegSetValueExLong(lngHwdKey, strValueName, _
0&, lngType, lngValue, 4)

End Select
End Function

Public Function QueryKeyValue(ByVal strSectionName As String, ByVal _
strKeyName As String, Optional ByVal lngRegDataType As RegDataType _
= HKEY_LOCAL_MACHINE) As Long

'Declare variables
Dim lngRC As Long
Dim lngHwd As Long
Dim varValue As Variant

'Get the value currently in the key and close it
lngRC = RegOpenKeyEx(lngRegDataType, strSectionName, 0, _
KEY_ALL_ACCESS, lngHwd)

m_strKeyHandle = lngHwd 'set key handle

lngRC = QueryValueEx(lngHwd, strKeyName, varValue)
RegCloseKey (lngHwd)

'Return with the return code
QueryKeyValue = lngRC
End Function

Private Function QueryValueEx(ByVal lngHwdKey As Long, ByVal strKeyName _
As String, ByVal varValue As Variant) As Long

'Declare variables
Dim lngDataLen As Long
Dim lngRC As Long
Dim lngType As Long
Dim lngTemp As Long
Dim strTemp As String

On Error GoTo QueryErr

'Determine the size and type of data to be read
lngRC = RegQueryValueExNULL(lngHwdKey, strKeyName, 0&, lngType, _
0&, lngDataLen)
If lngRC <> 0 Then Error 5

Select Case lngType

'Strings
Case REG_SZ:
strTemp = String(lngDataLen, 0)
lngRC = RegQueryValueExString(lngHwdKey, strKeyName, 0&, _
lngType, strTemp, lngDataLen)
If lngRC = 0 Then
m_strKeyValue = Left$(strTemp, lngDataLen - 1)
Else
m_strKeyValue = Empty
End If

'DWORDs
Case REG_DWORD:
lngRC = RegQueryValueExLong(lngHwdKey, strKeyName, 0&, _
lngType, lngTemp, lngDataLen)
If lngRC = 0 Then
m_lngKeyValue = lngTemp
Else
m_lngKeyValue = -1
End If

'All other data types
Case Else
lngRC = -1

End Select

On Error GoTo 0
QueryValueEx = lngRC
Exit Function

QueryErr:
lngRC = -1
QueryValueEx = lngRC
End Function

Public Function DeleteKey(lngRegDataType As Long, strKey As String) As Long
Dim lngValue As Long
lngValue = RegDeleteKey(lngRegDataType, strKey)
DeleteKey = lngValue
End Function

Public Function DeleteKeyValue(ByVal strSectionName As String, ByVal _
strKeyName As String, Optional ByVal lngRegDataType As RegDataType _
= HKEY_LOCAL_MACHINE) As Long

'Declare variables
Dim lngRC As Long
Dim lngHwd As Long
Dim varValue As Variant

'Get the value currently in the key and close it
lngRC = RegOpenKeyEx(lngRegDataType, strSectionName, 0, _
KEY_ALL_ACCESS, lngHwd)

lngRC = RegDeleteValue(lngHwd, strKeyName)

RegCloseKey (lngHwd)

'Return with the return code
DeleteKeyValue = lngRC
End Function

Private Sub Class_Initialize()
'MsgBox "class init"
End Sub

Private Sub Class_Terminate()
'MsgBox "class terminate"
End Sub
imkeby 2009-03-01
  • 打赏
  • 举报
回复
给你发个注册表常用操作的模块和使用示例:

'Declare private variables
Private m_lngKeyValue As Long
Private m_strKeyValue As String
Private m_strKeyHandle As Long

'Declare enumerations
Public Enum RegDataType
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_PERFORMANCE_DATA = &H80000004
HKEY_CURRENT_CONFIG = &H80000005
HKEY_DYN_DATA = &H80000006
End Enum
使用示例
[code=VB]
Private Sub Command1_Click()
Dim i As Long
Set cReg = New clsRegistry
i = cReg.CreateKey("Network\", "Temp", HKEY_CURRENT_USER)
If i = 0 Then
MsgBox "New key created."
Else
MsgBox "Couldn't create key."
End If
Set cReg = Nothing
End Sub

Public Enum KeyTypes
REG_SZ = 1
REG_DWORD = 4
End Enum

'Declare private constants
Private Const KEY_ALL_ACCESS = &H3F
Private Const REG_OPTION_NON_VOLATILE = 0

'Declare registry APIs
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) _
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, ByVal lpSecurityAttributes As _
Long, 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 ulOptions As Long, ByVal samDesired As Long, phkResult _
As Long) As Long

Private Declare Function RegQueryValueExString Lib "advapi32.dll" 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 RegQueryValueExLong Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal lpReserved As Long, lpType As Long, lpData As _
Long, lpcbData As Long) As Long

Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
As Long, lpcbData As Long) As Long

Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal Reserved As Long, ByVal dwType As Long, ByVal _
lpValue As String, ByVal cbData As Long) As Long

Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As _
String, ByVal Reserved As Long, ByVal dwType As Long, lpValue _
As Long, ByVal cbData 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 RegDeleteKey Lib "advapi32.dll" Alias _
"RegDeleteKeyA" (ByVal hKey As Long, ByVal lpKeyName As String) As Long

Public Property Get KeyHandle() As Long
KeyHandle = m_strKeyHandle
End Property

Public Property Get StringKeyValue() As String
StringKeyValue = m_strKeyValue
End Property

Public Property Get LongKeyValue() As Long
LongKeyValue = m_lngKeyValue
End Property

Public Function CreateKey(ByVal strSectionName As String, ByVal strKey As _
String, Optional ByVal lngRegDataType As RegDataType = _
HKEY_LOCAL_MACHINE) As Long

'Create a new key
CreateKey = CreateKeyEx(strSectionName & strKey, lngRegDataType)
End Function
Private Function CreateKeyEx(strSectionName As String, Optional _
lngRegDataType As Long = HKEY_LOCAL_MACHINE) As Long

'Declare variables
Dim lngHwd As Long
Dim lngRC As Long

'Create the key and close it
lngRC = RegCreateKeyEx(lngRegDataType, strSectionName, 0&, _
vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, lngHwd, _
lngRC)
RegCloseKey (lngHwd)

'Return with the return code
CreateKeyEx = lngRC
End Function

Public Function SetKeyValue(ByVal strSectionName As String, ByVal _
strKeyName As String, ByVal varKeyData As Variant, ByVal lngKeyType _
As KeyTypes, Optional ByVal lngRegDataType As RegDataType = _
HKEY_LOCAL_MACHINE) As Long

'Declare variables
Dim lngRC As Long
Dim lngHwd As Long

'Open the key, set the value and close the key
lngRC = RegOpenKeyEx(lngRegDataType, strSectionName, 0, _
KEY_ALL_ACCESS, lngHwd)
lngRC = SetValueEx(lngHwd, strKeyName, lngKeyType, varKeyData)
RegCloseKey (lngHwd)

'Return with the return code
SetKeyValue = lngRC
End Function
[/code]
happy_sea 2009-03-01
  • 打赏
  • 举报
回复
我pia.pia.pia,来晚lie
axibi 2009-03-01
  • 打赏
  • 举报
回复
....buhui
僵哥 2009-02-26
  • 打赏
  • 举报
回复
RegCreateKeyEx可以建立多级。
SYSSZ 2009-02-26
  • 打赏
  • 举报
回复
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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long


Private Sub Command1_Click()
openregery
End Sub


Private Sub openregery()
Dim ret As Long, hKey As Long, hKey2 As Long
ret = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft", hKey)
If ret = 0 Then
MsgBox "HKLM\SOFTWARE\Microsoft = " & hKey
End If
ret = RegOpenKey(hKey, "Windows\CurrentVersion", hKey2)
If ret = 0 Then
MsgBox "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion = " & hKey2
End If
'Use RegCreateKey function to create subkey "HKEY_LOCAL_MACHINE\SOFTWARE\Hongqt"
ret = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Hongqt", hKey)
If Not ret Then
MsgBox "新建 HKEY_LOCAL_MACHINE\SOFTWARE\Hongqt SubKey 成功"
Else
MsgBox "新建子键的操作失败"
End If
RegCloseKey hKey
RegCloseKey hKey2
End Sub
东方之珠 2009-02-26
  • 打赏
  • 举报
回复
http://zhidao.baidu.com/question/34480230.html

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧