操作注册表-黄海
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> Access数据库-模块/函数/VBA


操作注册表

发表时间:2004/11/20 评论(0) 浏览(7459)  评论 | 加入收藏 | 复制
   
摘 要:操作注册表
正 文:

 

Option ExplicitDeclare 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 LongDeclare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As LongDeclare 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 LongDeclare 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 LongDeclare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As Long, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long

Public Const SYNCHRONIZE = &H100000Public Const READ_CONTROL = &H20000Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)

Public Const KEY_QUERY_VALUE = &H1Public Const KEY_ENUMERATE_SUB_KEYS = &H8Public Const KEY_NOTIFY = &H10Public Const KEY_SET_VALUE = &H2Public Const KEY_CREATE_SUB_KEY = &H4

Public Const KEY_READ = ((READ_CONTROL Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))

Public Const REG_SZ = 1Public Const REG_BINARY = 3Public Const REG_DWORD = 4

Public Const HKEY_LOCAL_MACHINE = &H80000002Public Const REG_OPENED_EXISTING_KEY = &H2__________________________________________________

'在需要时建立主键Function CreateRegistryKey(ByVal hkey As Long, ByVal KeyName As String) As BooleanDim Handle As Long, Disposition As LongIf RegCreateKeyEx(hkey, KeyName, 0, 0, 0, 0, 0, Handle, Disposition) ThenMsgBox "不能建立该键", , "注意"Else' Return True if the key already existed.If Disposition = REG_OPENED_EXISTING_KEY Then CreateRegistryKey = True' Close the key.RegCloseKey HandleEnd IfEnd Function

'读取注册表的键值Function GetRegistryValue(ByVal hkey As Long, ByVal KeyName As String, _ByVal ValueName As String, ByVal KeyType As Integer, _Optional DefaultValue As Variant = Empty) As Variant

Dim Handle As Long, resLong As LongDim resString As String, length As LongDim resBinary() As Byte

' Prepare the default result.GetRegistryValue = DefaultValue' Open the key, exit if not found.If RegOpenKeyEx(hkey, KeyName, 0, KEY_READ, Handle) Then Exit Function

Select Case KeyTypeCase REG_DWORD' Read the value, use the default if not found.If RegQueryValueEx(Handle, ValueName, 0, REG_DWORD, _resLong, 4) = 0 ThenGetRegistryValue = resLongEnd IfCase REG_SZlength = 1024: resString = Space$(length)If RegQueryValueEx(Handle, ValueName, 0, REG_SZ, _ByVal resString, length) = 0 Then' If value is found, trim characters in excess.GetRegistryValue = Left$(resString, length - 1)End IfCase REG_BINARYlength = 4096ReDim resBinary(length - 1) As ByteIf RegQueryValueEx(Handle, ValueName, 0, REG_BINARY, _resBinary(0), length) = 0 ThenReDim Preserve resBinary(length - 1) As ByteGetRegistryValue = resBinary()End IfCase ElseMsgBox "不支持的类型", , "注意"End Select

RegCloseKey HandleEnd Function

'设置注册表的键值' Write / Create a Registry value.' Use KeyName = "" for the default value.' Supports only DWORD, SZ, and BINARY value types.

Sub SetRegistryValue(ByVal hkey As Long, ByVal KeyName As String, ByVal ValueName As String, ByVal KeyType As Integer, value As Variant)Dim Handle As Long, lngValue As Long, Disposition As LongDim strValue As StringDim binValue() As Byte, le


Access软件网交流QQ群(群号:198465573)
 
 相关文章
利用批处理文件自动修改注册表完成access宏安全级别的设置,利用...  【drchen  2010/8/9】
通过注册表修改Excel安全级别  【纵云梯  2012/8/11】
如何读取注册表中的信息  【周芳(转)  2012/10/23】
VBA创建删除,获取注册表信息\操作注册表  【叶海峰  2012/12/19】
应用程序开机自动启动(注册表操作技巧)  【转载文章  2013/11/13】
教您如何彻底删除SQL Server注册表  【缪炜  2015/4/6】
常见问答
技术分类
相关资源
文章搜索
关于作者

黄海

文章分类

文章存档

友情链接