首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

怎么读取注册表中的默认值

2012-01-11 
如何读取注册表中的默认值注册表项为:HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Hhc.File\shell\open\command

如何读取注册表中的默认值
注册表项为:HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Hhc.File\shell\open\command
下面有一个默认值。从MSDN中查看到资料,使用RegQueryValueEx,将其中的lpValue设置为""就可以读取了。但写了以下代码总是读不出来,请高手帮忙看下问题在那里?
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_CLASSES_ROOT = &H80000000
hhcFilePath = QueryRegKeyValue(HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Hhc.File\shell\open\command", "")

VB code
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 LongPrivate Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongFunction QueryRegKeyValue(RootNodeName As Long, NodePath As String, NodeName As String) As String    Dim tmp    As Long    Dim S     As String    Dim lenData     As Long    Dim typeData     As Long    Dim rc     As String    ret = RegOpenKey(RootNodeName, NodePath, tmp)    ret = RegQueryValueEx(tmp, NodeName, ByVal 0, typeData, ByVal vbNullString, lenData)    S = String(lenData, Chr(0))    If S = "" Then    QueryRegKeyValue = ""    Exit Function    Else    QueryRegKeyValue = Left(S, InStr(S, Chr(0)) - 1)'    RegQueryValueEx tmp, NodeName, ByVal 0, typeData, ByVal S, lenData    End IfEnd Function



[解决办法]
你这个代码毛病比较多,下面的代码读取了HKEY_CLASSES_ROOT\.7z的默认值的数据:
Option Explicit
Const HKEY_CLASSES_ROOT = &H80000000
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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Const REG_SZ = 1




Private Sub Command1_Click()
Dim ret As Long
Dim key As Long
Dim length As Long
ret = RegOpenKey(HKEY_CLASSES_ROOT, ".7z", key)
'先取数据区的长度
ret = RegQueryValueEx(key, "", 0, REG_SZ, ByVal 0, length)
'准备数据区
Dim buff() As Byte
ReDim buff(length - 1)
'读取数据
ret = RegQueryValueEx(key, "", 0, REG_SZ, buff(0), length)
Dim val As String
'去掉末尾的空字符,VB不需要这个
ReDim Preserve buff(length - 2)
'转化为VB中的字符串
val = StrConv(buff, vbUnicode)
RegCloseKey (key)
End Sub

[解决办法]
我把你的代码改了一下:
Private Const HKEY_CLASSES_ROOT = &H80000000

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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Function QueryRegKeyValue(RootNodeName As Long, NodePath As String, NodeName As String) As String
Dim tmp As Long
Dim S As String
Dim lenData As Long
Dim typeData As Long
Dim rc As String
ret = RegOpenKey(RootNodeName, NodePath, tmp)
ret = RegQueryValueEx(tmp, NodeName, ByVal 0, typeData, ByVal vbNullString, lenData)


S = String(lenData, Chr(0))
RegQueryValueEx tmp, NodeName, ByVal 0, typeData, ByVal S, lenData
If S = "" Then
QueryRegKeyValue = ""
Exit Function
Else
QueryRegKeyValue = Left$(S, InStr(S, ChrW$(0)) - 1)

End If
End Function


Private Sub Command1_Click()
QueryRegKeyValue HKEY_CLASSES_ROOT, ".323", vbNullString

End Sub

热点排行