VB怎么操作注册表?VB注册表操作函数

'模块代码声明
'==================================================
'注册表操作声明
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public 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
Public Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Public 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
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Private Const REG_SZ = 1&
Private Const REG_EXPAND_SZ = 2&
Private Const REG_BINARY = 3&
Private Const REG_DWORD = 4&
Private Const ERROR_SUCCESS = 0&
'==================================================

'================================
'注册表操作函数
'================================


'读取注册表字符串键值
Public Function GetString(hKey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
Dim lValueType As Long 'new add
RegOpenKey hKey, strPath, keyhand
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ or lValueType = REG_EXPAND_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetString = left$(strBuf, intZeroPos - 1)
Else: GetString = strBuf
End If
End If
End If
End Function

'写入注册表字符串键值
Public Sub SetString(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
RegCreateKey hKey, strPath, keyhand
RegSetValueEx keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata)
RegCloseKey keyhand
End Sub

'读取注册表 DWORD 键值
Function GetDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long

r = RegOpenKey(hKey, strPath, keyhand)

' Get length/data type
lDataBufSize = 4

lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)

If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then
GetDword = lBuf
End If
'Else
' Call errlog("GetDWORD-" & strPath, False)
End If

r = RegCloseKey(keyhand)
End Function

'写入注册表 DWORD 键值
Function SetDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
Dim keyhand As Long
RegCreateKey hKey, strPath, keyhand
RegSetValueEx keyhand, strValueName, 0&, REG_DWORD, lData, 4
RegCloseKey keyhand
End Function

'读取注册表二进制键值
Function GetBinary(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long

r = RegOpenKey(hKey, strPath, keyhand)

' Get length/data type
lDataBufSize = 4

lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)

If lResult = ERROR_SUCCESS Then
If lValueType = REG_BINARY Then
GetBinary = lBuf
End If
End If

r = RegCloseKey(keyhand)
End Function

'写入注册表二进制键值
Function SetBinary(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long, ByVal BitNumber As Long)
Dim keyhand As Long
RegCreateKey hKey, strPath, keyhand
RegSetValueEx keyhand, strValueName, 0&, REG_BINARY, lData, BitNumber
RegCloseKey keyhand
End Function

'删除一个注册表键值
Public Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
RegOpenKey hKey, strPath, keyhand
RegDeleteValue keyhand, strValue
RegCloseKey keyhand
End Function

'创建一个主键
Public Function CreateKey(ByVal hKey As Long, ByVal strKey As String)
Dim keyhand&
RegCreateKey hKey, strKey, keyhand
RegCloseKey keyhand&
End Function

上一篇: VB实现HOOK!VB无DLL Hook API
下一篇: VB怎么做浏览器插件?VB怎么做BHO插件
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 1476 | 引用: 0 | 查看次数: 31796
Jackie[2018-03-26 09:47 PM | | Mail To:jackiesummerfield@gmail.com | 181.177.112.202 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Lorna[2018-03-23 11:03 AM | | Mail To:lorna.moffit@gmail.com | 5.157.33.7 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Leatha[2018-03-13 03:18 AM | | Mail To:leathawhitlock@gawab.com | 186.47.102.166 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Jorg[2018-03-12 09:55 AM | | Mail To:jorgcarlin@yahoo.de | 203.77.230.114 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Aisha[2018-03-09 00:58 AM | | Mail To:aisha.goodson@gmail.com | 114.125.122.215 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Julian[2017-06-10 07:48 PM | | Mail To:julian_woods@inbox.com | 163.121.188.3 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Christi[2017-05-25 07:07 PM | | Mail To:christi_dickinson@gmail.com | 1.32.43.81 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Lavern[2017-05-09 06:23 AM | | Mail To:laverngillon@yahoo.de | 212.129.50.36 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Doreen[2017-05-03 09:42 PM | | Mail To:doreeniverson@gmail.com | 212.129.50.36 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
Tanisha[2017-05-01 04:21 AM | | Mail To:tanishawinkler@t-online.de | 52.58.249.129 | del | 通过审核 | 回复回复]
[此评论正在审核中,只有博主及评论作者可见]
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 关闭 | [img]标签 关闭