求一个操作注册表的可用例子行吗?
想求一个确实可用的操作注册表的例子参考.
我想先能判断注册表中是不是有我要注册的消息,没有就创建注册项;有注册消息就读取/修改它,删除的时候也能删除掉注册消息.这样可以能直接使用的例子有吗?
[解决办法]
REG.BAS
Attribute VB_Name = "basReg"
Option Explicit
' Reg Data Types...
Public Const REG_NONE = 0 ' No defined value type.
Public Const REG_SZ = 1 ' A null-terminated string. It will be a Unicode or ANSI string, depending on whether you use the Unicode or ANSI functions.
Public Const REG_EXPAND_SZ = 2 ' A null-terminated string that contains unexpanded references to environment variables (for example, "%PATH%"). It will be a Unicode or ANSI string depending on whether you use the Unicode or ANSI functions.
Public Const REG_BINARY = 3 ' Free form binary
Public Const REG_DWORD = 4 ' 32-bit number
Public Const REG_DWORD_LITTLE_ENDIAN = 4 ' A 32-bit number in little-endian format (same as REG_DWORD). In little-endian format, the most significant byte of a word is the high-order word. This is the most common format for computers running Windows NT and Windows 95.
Public Const REG_DWORD_BIG_ENDIAN = 5 ' A 32-bit number in big-endian format. In big-endian format, the most significant byte of a word is the low-order word.
Public Const REG_LINK = 6 ' A Unicode symbolic link.
Public Const REG_MULTI_SZ = 7 ' An array of null-terminated strings, terminated by two null characters.
Public Const REG_RESOURCE_LIST = 8 ' A device-driver resource list.
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9 ' Resource list in the hardware description
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
' Reg Create Type Values...
Public Const REG_OPTION_RESERVED = 0 ' Parameter is reserved
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Public Const REG_OPTION_VOLATILE = 1 ' Key is not preserved when system is rebooted
Public Const REG_OPTION_CREATE_LINK = 2 ' Created key is a symbolic link
Public Const REG_OPTION_BACKUP_RESTORE = 4 ' open for backup or restore
' Reg Key Security Options...
Public Const READ_CONTROL = &H20000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Public Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Public Const KEY_EXECUTE = KEY_READ
Public 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
' Reg Key ROOT Types...
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
'Registry Types
Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End Type
Public Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegEnumKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Public Declare Function RegEnumValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Public Declare Function RegOpenKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegQueryInfoKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, 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
Public Declare Function RegQueryValueExA Lib "advapi32.dll" (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 RegSetValueExA Lib "advapi32.dll" (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
Sub Main()
End Sub
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Service"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Option Explicit
Public Function CloseKey(hKey As Long) As Boolean
If RegCloseKey(hKey) Then
CloseKey = False
Else
CloseKey = True
End If
End Function
Public Function CreateKey(hKey As Long, sSubKey As String, sClass As String) As Long
Dim hKeyBuffer As Long
Dim lErr As Long
Dim lAccessDesired As Long
Dim lOptions As Long
Dim lDisposition As Long
lAccessDesired = KEY_ALL_ACCESS
lOptions = 0
lDisposition = 0
If RegCreateKeyExA(hKey, sSubKey, 0, sClass, lOptions, lAccessDesired, vbNull, hKeyBuffer, lDisposition) Then
CreateKey = False
Else
CreateKey = hKeyBuffer
End If
End Function
Public Function DeleteKey(hKey As Long, sSubKey As String) As Boolean
'Win95 will delete a Registry Key and all of it's descendants.
'WinNT will delete a Registry Key ONLY if NO descendants are present.
If RegDeleteKey(hKey, sSubKey) Then
DeleteKey = False
Else
DeleteKey = True
End If
End Function
Public Function DeleteValue(hKey As Long, sName As String) As Boolean
If RegDeleteValue(hKey, sName) Then
DeleteValue = False
Else
DeleteValue = True
End If
End Function
Public Function GetKey(hKey As Long, sSubKey As String) As Long
Dim lAccessDesired As Long
Dim hKeyBuffer As Long
lAccessDesired = KEY_ALL_ACCESS
If RegOpenKeyExA(hKey, sSubKey, 0, lAccessDesired, hKeyBuffer) Then
GetKey = False
Else
GetKey = hKeyBuffer
End If
End Function
Public Function GetValue(hKey As Long, sName As String) As String
Dim sData As String
sData = Space(254)
If RegQueryValueExA(hKey, sName, 0, REG_SZ, ByVal sData, Len(sData)) Then
GetValue = ""
Else
GetValue = Left$(sData, InStr(sData, Chr$(0)) - 1)
End If
End Function
Public Function SetValue(hKey As Long, sName As String, sData As String) As Boolean
If RegSetValueExA(hKey, sName, 0, REG_SZ, ByVal sData, Len(sData)) Then
SetValue = False
Else
SetValue = True
End If
End Function
RegWrite后面的参数,如果是\结尾就代表文件夹一样,否则就代表里面的一个键值