请问怎样得到注册表的键值?

diudiu2002 2003-05-19 08:24:41
我用RegQueryValueEx这个API函数能够得到注册表字符型的键值,请问怎样得到十六进制键值?
...全文
56 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
diudiu2002 2003-05-20
to anosoft(全方):好象getSetting只能读saveSetting的值吧!
to ufozq77(ZQ):好象没有这个函数吧!
回复
junwhj 2003-05-19
Option Explicit

Private Declare Function RegOpenKeyEx Lib "advapi32" _
Alias "RegOpenKeyExA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
ByRef phkResult As Long) As Long

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

Private Declare Function RegCloseKey Lib "advapi32" _
(ByVal hKey As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)

Private Const READ_CONTROL = &H20000
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL

Private Const HKEY_CURRENT_USER = &H80000001
Private Const ERROR_SUCCESS = 0
Private Const REG_SZ = 1
Private Const REG_BINARY = 3
Private Const REG_DWORD = 4


Private Sub Command1_Click()
Dim strBinary As String
'HKEY_CURRENT_USER
' +-----Test
' +----(默认) REG_SZ
' +----TestBinary REG_BINARY EB 90 56 58
If GetKeyValue(HKEY_CURRENT_USER, "Test", "TestBinary", strBinary) Then
MsgBox strBinary
End If
End Sub


Private Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String, ByRef KeyVal As String) As Boolean
Dim i As Long
Dim rc As Long
Dim hKey As Long
Dim hDepth As Long
Dim KeyValType As Long
Dim tmpVal As String
Dim KeyValSize As Long
Dim arrBinary() As Byte

rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey)

If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError

tmpVal = String$(1024, 0)
KeyValSize = 1024

rc = RegQueryValueEx(hKey, SubKeyRef, 0, KeyValType, tmpVal, KeyValSize)

If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError

If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then
tmpVal = Left(tmpVal, KeyValSize - 1)
Else
tmpVal = Left(tmpVal, KeyValSize)
End If

Select Case KeyValType
Case REG_SZ
KeyVal = tmpVal
Case REG_DWORD
For i = Len(tmpVal) To 1 Step -1
KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1)))
Next
KeyVal = Format$("&H" + KeyVal)
Case REG_BINARY
ReDim arrBinary(1 To KeyValSize)
Call CopyMemory(arrBinary(1), ByVal tmpVal, KeyValSize)
For i = 1 To KeyValSize
KeyVal = KeyVal & " " & Right("0" & Hex(arrBinary(i)), 2)
Next i
End Select

GetKeyValue = True
rc = RegCloseKey(hKey)
Exit Function

GetKeyError:
KeyVal = ""
GetKeyValue = False
rc = RegCloseKey(hKey)
End Function
回复
ufozq77 2003-05-19
MsgBox GetDWORDValue("HKEY_LOCAL_MACHINE", "DWORD Value")
回复
anosoft 2003-05-19
用GetSetting函数试试
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7490

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2003-05-19 08:24
社区公告
暂无公告