注册表中怎样读取二进制的键值?怎样写入?

lengxue 2000-08-25 08:57:00
加精
小弟刚学vb不久,就要搞这种玩意!真是难死我了!什么都是问题!
希望各位高手帮忙!
...全文
311 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我这儿有一个关于访问注册表的类,如果需要Email me: lumine@etang.com
shines77 2000-09-13
  • 打赏
  • 举报
回复
正确答案是:

Option Explicit

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 RegCloseKey Lib "advapi32.dll" (ByVal hKey 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 RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Const REG_SZ = 1 ' Unicode nul terminated string
Private Const REG_BINARY = 3 ' Free form binary
Private Const REG_DWORD = 4 ' 32-bit number

Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003

Private Sub Form_Load()
Dim YourString As String
'你要写入的字符串
YourString = "dd de df ab c3 9c 9c 9c" + _
"da ff df ab c3 9c 9c 9c" + _
"ee de df ab c3 9c 9c 9c" + _
"aa de df ab c3 9c 9c 99"
Dim i As Long
Dim Length As Long
Dim strByte() As Byte
Length = Int((Len(YourString) - 1) / 3)
ReDim strByte(Length)
For i = 0 To Length
strByte(i) = CByte(Val("&H" + Mid(YourString, i * 3 + 1, 2)))
Next i
'写入字符串
SaveString HKEY_CURRENT_USER, "RemoteAccess\Addresses", "xinjun", strByte
End Sub

Public Sub SaveString(hKey As Long, strPath As String, strValue As String, strData() As Byte)
Dim KeyHand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, KeyHand)
r = RegOpenKey(hKey, strPath, KeyHand)
r = RegSetValueEx(KeyHand, strValue, 0, REG_BINARY, strData(0), UBound(strData) - LBound(strData) + 1)
r = RegCloseKey(KeyHand)
End Sub
shines77 2000-08-25
  • 打赏
  • 举报
回复
我来给你例子吧!很详细!希望能解决你的问题:

Option Explicit

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 Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Const REG_SZ = 1 ' Unicode nul terminated string
Private Const REG_BINARY = 3 ' Free form binary
Private Const REG_DWORD = 4 ' 32-bit number

Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003
Private Const HKEY_PERFORMANCE_DATA = &H80000004
Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const HKEY_DYN_DATA = &H80000006

Private Const ERROR_SUCCESS = 0&
Private Const APINULL = 0&

Dim Dummy As Long

Private Sub Form_Load()
'写入Binary
Dummy = SetRegValueExDW_BIN(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "ShineBin", 1024, REG_BINARY)
End Sub

Function SetRegValueExDW_BIN(ByVal hKey_Kind As Long, ByVal KeyPath As String, ByVal strValueName As String, ByVal lngData As Long, ByVal DataType As Long) As Boolean
Dim hKey As Long
Dim lResult As Long
On Error Resume Next
lResult = RegOpenKey(hKey_Kind, KeyPath, hKey)
If hKey = 0 Then
'程序出现错误!
SetRegValueExDW_BIN = False
Exit Function
End If
lResult = RegSetValueEx(hKey, strValueName, 0&, DataType, lngData, 4)
RegCloseKey (hKey)
If lResult = ERROR_SUCCESS Then
SetRegValueExDW_BIN = True
Else
SetRegValueExDW_BIN = False
End If
End Function
U皮特U 2000-08-25
  • 打赏
  • 举报
回复
用RegQueryValueEx和RegSetValueEx函数。将其中dwType参数设为REG_BINARY。
Un1 2000-08-25
  • 打赏
  • 举报
回复
http://expert.csdn.net/Topic/26734.shtm

7,762

社区成员

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

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