菜鸟发问了,关于注册表的

1v1chen 2004-10-13 10:38:45
请各位高手
把在vb中操作注册标的方法贴出来吧
api的 wsh的,还有别的好办法
谢谢了
...全文
88 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
1v1chen 2004-10-13
模块式
' 这个模块用于读和写注册表关键字。
' 不同于VB 的内部注册表访问方法,它可以
' 通过字符串的值来读和写任何注册表关键字。


Option Explicit
'---------------------------------------------------------------
'-注册表 API 声明...
'---------------------------------------------------------------
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" 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, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
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 RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long

'---------------------------------------------------------------
'- 注册表 Api 常数...
'---------------------------------------------------------------
' Reg Data Types...
Const REG_SZ = 1 ' Unicode空终结字符串
Const REG_EXPAND_SZ = 2 ' Unicode空终结字符串
Const REG_DWORD = 4 ' 32-bit 数字

' 注册表创建类型值...
Const REG_OPTION_NON_VOLATILE = 0 ' 当系统重新启动时,关键字被保留

' 注册表关键字安全选项...
Const READ_CONTROL = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Const KEY_EXECUTE = KEY_READ
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

' 注册表关键字根类型...
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004

' 返回值...
Const ERROR_NONE = 0
Const ERROR_BADKEY = 2
Const ERROR_ACCESS_DENIED = 8
Const ERROR_SUCCESS = 0

'---------------------------------------------------------------
'- 注册表安全属性类型...
'---------------------------------------------------------------
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type

'-------------------------------------------------------------------------------------------------
'sample usage - Debug.Print UpodateKey(HKEY_CLASSES_ROOT, "keyname", "newvalue")
'-------------------------------------------------------------------------------------------------
Public Function UpdateKey(KeyRoot As String, KeyName As String, SubKeyName As String, SubKeyValue As String) As Boolean
Dim rc As Long ' 返回代码
Dim hKey As Long ' 处理一个注册表关键字
Dim hDepth As Long '
Dim lpAttr As SECURITY_ATTRIBUTES ' 注册表安全类型

lpAttr.nLength = 50 ' 设置安全属性为缺省值...
lpAttr.lpSecurityDescriptor = 0 ' ...
lpAttr.bInheritHandle = True ' ...

'------------------------------------------------------------
'- 创建/打开注册表关键字...
'------------------------------------------------------------
rc = RegCreateKeyEx(KeyRoot, KeyName, _
0, REG_SZ, _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
hKey, hDepth) ' 创建/打开//KeyRoot//KeyName

If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' 错误处理...

'------------------------------------------------------------
'- 创建/修改关键字值...
'------------------------------------------------------------
If (SubKeyValue = "") Then SubKeyValue = " " ' 要让RegSetValueEx() 工作需要输入一个空格...

' 创建/修改关键字值
rc = RegSetValueEx(hKey, SubKeyName, _
0, REG_SZ, _
SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode)))

If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' 错误处理
'------------------------------------------------------------
'- 关闭注册表关键字...
'------------------------------------------------------------
rc = RegCloseKey(hKey) ' 关闭关键字

UpdateKey = True ' 返回成功
Exit Function ' 退出
CreateKeyError:
UpdateKey = False ' 设置错误返回代码
rc = RegCloseKey(hKey) ' 试图关闭关键字
End Function

回复
1v1chen 2004-10-13
Private Sub Form_Load()
Dim xx
Dim dmoObj As New SQLDMO.Application
Dim default
Dim i As Long
Server = UpdateKey(HKEY_CLASSES_ROOT, "keyname", "newvalue")
MsgBox Server
If Server = "" Then
'MsgBox dmoObj.ListAvailableSQLServers.Count
For i = 1 To dmoObj.ListAvailableSQLServers.Count
ListView1.ListItems.Add , , dmoObj.ListAvailableSQLServers.Item(i)
Next
Else
End If
'err1:
End Sub
有什么问题吗?
回复
1v1chen 2004-10-13
应该还有
接着顶
回复
ziran666 2004-10-13
VB中操作注册表需要用到的API常量及函数:
Private Const HKEY_CLASSES_ROOT = &H80000000

Private Const HKEY_CURRENT_CONFIG = &H80000005

Private Const HKEY_CURRENT_USER = &H80000001

Private Const HKEY_DYN_DATA = &H80000006

Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Const HKEY_PERFORMANCE_DATA = &H80000004

Private Const HKEY_USERS = &H80000003


Private Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long

Private Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" (ByVal hKey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal hEvent As Long, ByVal fAsynchronus 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 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 RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, ByVal lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long

Private 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

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 ' Note that if you declare the lpData parameter as String, you must pass it By Value.

Private Declare Function RegReplaceKey Lib "advapi32.dll" Alias "RegReplaceKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpNewFile As String, ByVal lpOldFile As String) As Long

Private Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long

Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long

Private Declare Function RegSetKeySecurity Lib "advapi32.dll" (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR) As Long

Private 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

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 ' Note that if you declare the lpData parameter as String, you must pass it By Value.

Private Declare Function RegUnLoadKey Lib "advapi32.dll" Alias "RegUnLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Public Type ACL
AclRevision As Byte
Sbz1 As Byte
AclSize As Integer
AceCount As Integer
Sbz2 As Integer
End Type



Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Public Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As ACL
Dacl As ACL
End Type

Public Type SECURITY_QUALITY_OF_SERVICE
Length As Long
Impersonationlevel As Integer
ContextTrackingMode As Integer
EffectiveOnly As Long
End Type


可查阅VBAPI文档,搞清每个函数的功能,在程序中调用即可。
回复
tztz520 2004-10-13

RegQueryValueEx--读取某Key的特定名称的值(Value)
Vb声明和参数解释:
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
hkey:Key Handle
lpValueName:Value Name
lpReserved:保留参数,调用时设置为0即可
lpType:返回读取的数据类型
lpData:返回读取的数据
lpcbData:传入lpData数据的长度,若成功读取数据,则返回所读取的数据的长度。
返回值: =0,表示成功;≠0,表示失败。
说明:
1、 这一函数除了可读取指定名称的值之外,也可以读取default value。如果要读取default value,只需要将
参数lpValueName设置为""[空字符串]即可。
2、lpType 的可能取值
Enum ValueType
REG_NONE = 0
REG_SZ = 1 -->字符串
REG_EXPAND_SZ = 2 -->可展开式字符串
REG_BINARY = 3 -->Binary数据
REG_DWORD = 4 -->长整数
REG_DWORD_BIG_ENDIAN = 5 -->BIG_ENDIAN长整数
REG_MULTI_SZ = 7 -->多重字符串
End Enum

e.g:
Dim hKey As Long, ret As Long, lenData As Long, typeData As Long
Dim Name As String
'读取HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run的internat.exe的value.
Name="internat.exe"
ret=RegOpenKey(HKEY_LOCAL_MACHINE,"Software\Microsoft\Windows\CurrentVersion\Run", hKey)
if ret=0 then
ret = RegQueryValueEx(hKey, Name, 0, typeData, ByVal vbNullString, lenData)'注意ByVal千万别忘了
end if
回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告