VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 用VB操作注册表(二)
发表评论(0)作者:梦里水乡, 平台:VB6.0+Win98, 阅读:10472, 日期:2001-02-13
用VB操作注册表(二)
梦里水乡

登录数据库Registry的Value的存取--Default value的存取

1、Default value的存取
我曾经给我的朋友开了个玩笑,他最讨厌喜欢windows中自带的一个扫雷的游戏了.我呢,编写了一个小程序叫他帮我测试一下有没有运行错误,他欣然接受了,结果在执行完毕之后,那个程序自己消失了[那段时间我正在研究"木马冰河",对程序的自销毁技术很感兴趣,想着法子也实现了这个功能.]以后他的机器一启动就要运行扫雷这个程序,气得他暴跳如雷......。其实这样的程序很容易实现的。我们在讲述了有关函数后将给出这个程序的完整例子

涉及到的API函数讲解:

RegQueryValue

VB声明
Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
hKey: Key Handle
lpSubKey:SubKey名称路径
lpValue:返回读取的Default Value
lpcbValue:传入lpValue参数的长度,若成功读取了默认值default value,则返回default value字符串的长度(含chr(0))这个和C语言中字符串的处理相似,都是以chr(0)作为结束符。

返回值: =0,表示成功;≠0,表示失败。


函数调用实例:
`自编函数GetDefaultValue
`读取Default Value,若成功,返回true
`Example:
`Dim S As String, ret As Boolean
`ret = GetDefaultValue(HKEY_CLASSES_ROOT, ".txt", S)
` 如果 ret 为 True(与我们正常的函数调用习惯相同), 則 S 等於读取之資料
` 如果 "HKEY_CLASSES_ROOT\.txt" 沒有缺省值, 則 S = ""

Function GetDefaultValue(ByVal hKey As Long, ByVal Subkey As String, Value As String) As Boolean
Dim ret As Long, lenS As Long, S As String
ret = RegQueryValue(hKey, Subkey, "", lenS)
If ret <> 0 And ret <> ERROR_MORE_DATA
Then GetDefaultValue = False
Exit Function
End If
S = String(lenS, Chr(0))
ret = RegQueryValue(hKey, Subkey, S, lenS)
If ret <> 0 Then
GetDefaultValue = False
Exit Function
End If
Value = Left(S, lenS - 1)
GetDefaultValue = True
End Function

上面讲的是default value的读取,下面我们讲述它的写入。它要用到API函数RegSetValue,它的详细讲解如下:
VB声明
Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
hKey:Key Handle
lpSubKey:Subkey名称或路径
dwType:数据类型,但在这里只能接受REG_SZ[字符串类型]
lpData:所设置的字符串
cbData:lpData字符串的长度,这一长度包括chr(0)字符。
关于dwType的可能取值
Enum ValueType
REG_NONE = 0
REG_SZ = 1
REG_EXPAND_SZ = 2
REG_BINARY = 3
REG_DWORD = 4
REG_DWORD_BIG_ENDIAN = 5
REG_MULTI_SZ = 7
End Enum
其具体含义我们在以后再讲。

函数调用实例:
`自编函数SetDefaultValue
`写入Default Value
`比较值得注意的事情是, 当我们想写入某一个 Subkey 的 Default Value 时,若此一 Subkey 不存在, 则 Windows 会自动建立此一 Subkey, 然后才写入 Default Value, 假设"HKEY_LOCAL_MACHINE\SOFTWARE\kj\Registry" Subkey 并不存在, 则以下敘述:
`ret = SetDefaultValue(HKEY_LOCAL_MACHINE, "SOFTWARE\kj\Registry", "kj Registry Master")
`会先建立以下两个 Subkey:(HKEY_LOCAL_MACHINE\SOFTWARE 为已存在的 Subkey) `HKEY_LOCAL_MACHINE\SOFTWARE\kj `HKEY_LOCAL_MACHINE\SOFTWARE\kj\Registry
`然后才写入 "kj Registry Master" 到 "HKEY_LOCAL_MACHINE\SOFTWARE\kj\Registry" Subkey 的 Default Value。

Function SetDefaultValue(ByVal hKey As Long, ByVal Subkey As String, ByVal Value As String) As Boolean
Dim ret As Long, lenS As Long, S As String
ret = RegSetValue(hKey, Subkey, REG_SZ, Value, LenB(StrConv(Value, vbFromUnicode)) + 1) SetDefaultValue = (ret = 0)
End Function

下面我举的一个完整的例子就是我在这篇文章开头提到的程序,只给出如何设置扫雷程序为开机自运行程序的例子,自销毁程序的设计比较复杂一些,暂不提供.

模块文件registry.bas

Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
Enum RootKey
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
Enum ErrorCode
ERROR_SUCCESS = 0&
ERROR_MORE_DATA = 234&
End Enum

Enum ValueType
REG_NONE = 0
REG_SZ = 1
REG_EXPAND_SZ = 2
REG_BINARY = 3
REG_DWORD = 4
REG_DWORD_BIG_ENDIAN = 5
REG_MULTI_SZ = 7
End Enum

Function SetDefaultValue(ByVal hKey As Long, ByVal Subkey As String, ByVal Value As String) As Boolean
Dim ret As Long, lenS As Long, S As String
ret = RegSetValue(hKey, Subkey, REG_SZ, Value, LenB(StrConv(Value, vbFromUnicode)) + 1) SetDefaultValue = (ret = 0)
End Function


Function GetDefaultValue(ByVal hKey As Long, ByVal Subkey As String, Value As String) As Boolean
Dim ret As Long, lenS As Long, S As String
`读取default value的字符串长度
ret = RegQueryValue(hKey, Subkey, "", lenS)
If ret <> 0 And ret <> ERROR_MORE_DATA
Then GetDefaultValue = False
Exit Function
End If
S = String(lenS, Chr(0)) `再根据上一个RegQueryValue返回的lenS值来配置字符串。
ret = RegQueryValue(hKey, Subkey, S, lenS)
If ret <> 0 Then
GetDefaultValue = False
Exit Function
End If
Value = Left(S, lenS - 1)
GetDefaultValue = True
End Function

然后在form中放置两个命令按钮command1和command2.
`command1_click要做的事就是设置扫雷程序为开机自启动程序。若操作成功,显示success对话框。
Private Sub Command1_Click()
Dim ret As Boolean
Dim disp As String
ret = SetDefaultValue(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "c:\windows\winmine.exe")
If ret Then
disp = "Sucess!"
Else disp = "Fail"
End If
MsgBox disp, , "结果"
End Sub
`command2_click要做的事情就是读入HKEY_CLASSES_ROOT\.txt这个SubKey的Default Value。
Private Sub Command2_Click()
Dim S As String, ret As Boolean
Dim hKey As Long
Dim tmpstr As String
Dim disp As String
tmpstr = "The defaultvalue of HKEY_CLASSES_ROOT\.txt is: "
ret = RegOpenKey(HKEY_CLASSES_ROOT, ".txt", hKey)
ret = GetDefaultValue(hKey, "", S)
If ret Then
If S <> "" Then
disp = tmpstr & S
Else
disp = tmpstr & "NoDefaultValue"
End If
End If
MsgBox disp, , "结果"
End Sub