| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2999 人关注过本帖, 1 人收藏
标题:[原创]拿下注册表
只看楼主 加入收藏
redice
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:902
专家分:0
注 册:2006-12-11
结帖率:72.73%
收藏(1)
 问题点数:0 回复次数:8 
[原创]拿下注册表

大家好,下面是我写的关于注册表操作的常用函数,希望对大家有所帮助

'****************************注册表操作函数**********************
'声明:以下代码由轻风工作室REDICE编写,引用时请作一说明。
'****************************************************************

'*****下面先声明一些常量******************************************
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const KEY_ALL_ACCESS = (&H20000 Or &H1 Or &H2 Or &H4 Or &H8 Or &H10 Or &H20) And (Not &H100000)
Public Const REG_SZ = 1
Public Const REG_DWORD = 4
'*****************************************************************

'*****下面声明注册表操作中用到的API函数****************************
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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public 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
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) 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 RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Public Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, 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 Function createnewkey(ip As Long, snewkeyname As String)
Dim hnewkey As Long
Dim retval As Long
retval = RegCreateKey(ip, snewkeyname, hnewkey)
If retval = 0 Then
RegCloseKey (hnewkey) '关闭上面建立或打开的项
End If
End Function
'实例:在HKEY_CURRENT_USER下建立项"xiaopeng"
'代码为 createnewkey HKEY_CURRENT_USER ,"xiaopeng"
'******************************************************************

'*******删除注册表项***********************************************
Public Function deletekey(ip As Long, skeyname As String)
Dim hKey As Long
Dim retval As Long
retval = RegOpenKeyEx(ip, skeyname, 0, KEY_ALL_ACCESS, hKey)
If retval = 0 Then
RegDeleteKey ip, skeyname
End If
End Function
'实例:删除上面建立的HKEY_CURRENT_USER下的项"xiaopeng"
'代码为 deletekey HKEY_CURRENT_USER ,"xiaopeng"
'******************************************************************

'********新建,设置数值名称*****************************************
Public Function setkeyvalue(ByVal ip As Long, ByVal keyname As String, ByVal valuename As String, ByVal valuesetting As Variant, ByVal valuetype As Long)
Dim retval As Long
Dim hKey As Long
If RegOpenKeyEx(ip, keyname, 0, KEY_ALL_ACCESS, hKey) > 0 Then Exit Function
Select Case valuetype
Case REG_SZ
RegSetValueExString hKey, valuename, 0&, REG_SZ, valuesetting, LenB(StrConv(valuesetting, vbFromUnicode))
Case REG_DWORD
RegSetValueExLong hKey, valuename, 0, valuetype, valuesetting, 4
End Select
RegCloseKey (hKey)
End Function
'实例:在HKEY_CURRENT_USER下的项"xiaopeng"中建立名为"redice",键值为"is xiaopeng",类型为REG_SZ的新键
'代码为 setkeyvalue HKEY_CURRENT_USER ,"xiaopeng" ,"redice","is xiaopeng",REG_SZ
'又如:在HKEY_CURRENT_USER下的项"xiaopeng"中建立名为"ceshi",键值为2,类型为REG_DWORD的新键
'代码为"setkeyvalue HKEY_CURRENT_USER,"xiaopeng","ceshi",2,REG_DWORD
'******************************************************************

'*********删除数值名称*********************************************
Public Function deletevalue(ByVal ip As Long, ByVal keyname As String, ByVal valuename As String)
Dim retval As Long
Dim hKey As Long
retval = RegOpenKeyEx(ip, keyname, 0, KEY_ALL_ACCESS, hKey)
If retval > 0 Then
Exit Function
End If
RegDeleteValue hKey, valuename
RegCloseKey hKey
End Function
'实例:删除HKEY_CURRENT_USER下的项"xiaopeng"中名为"redice"的新键
'代码为 deletevalue HKEY_CURRENT_USER ,"xiaopeng","redice"
'******************************************************************
'**********查询已存在的数值内容************************************
Public Function getvalue(ByVal ip As Long, keyname As String, valuename As String) As String
Dim retval As Long
Dim hKey As Long
Dim valuesetting As Variant
Dim cddata As Long
Dim lvalue As Long
Dim svalue As String
Dim lvaluetye As Long
retval = RegOpenKeyEx(ip, keyname, 0, KEY_ALL_ACCESS, hKey)
If retval > 0 Then
getvalue = ""
Exit Function
End If
retval = RegQueryValueEx(hKey, valuename, 0, lvaluetype, ByVal vbNullString, cddata)
If retval <> 0 Then
RegCloseKey hKey
Exit Function
End If
Select Case lvaluetype
Case REG_SZ
svalue = String(cddata, Chr(0))
RegQueryValueEx hKey, valuename, 0, lvaluetype, ByVal svalue, cddata
valuesetting = Left$(svalue, cddata)
getvalue = CStr(valuesetting)
Case REG_DWORD
RegQueryValueEx hKey, valuename, 0, lvaluetype, lvalue, cddata
valuesetting = lvalue
getvalue = CStr(valuesetting)
End Select
End Function
'实例:获取HKEY_CURRENT_USER下的项"xiaopeng"中名为"redice"的新键的键值
'代码为 getvalue HKEY_CURRENT_USER ,"xiaopeng","redice"
'*********************************************************************

[此贴子已经被作者于2007-11-8 21:08:34编辑过]

搜索更多相关主题的帖子: 注册表 Public Const HKEY 
2006-12-23 15:20
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
不错啊,支持一下

雁无留踪之意,水无取影之心
2006-12-23 19:57
gulangchow83
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2007-1-16
收藏
得分:0 
支持!!!
2007-01-22 22:18
川江
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-1-4
收藏
得分:0 
恩,不错
2007-01-26 14:54
XMeLoDy
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-10-4
收藏
得分:0 

如何添加REG_DWORD的数据类型到注册表啊??

2007-11-08 20:16
西风独自凉
Rank: 8Rank: 8
等 级:贵宾
威 望:43
帖 子:3380
专家分:28
注 册:2007-8-2
收藏
得分:0 
不錯,頂下。。。。

2007-11-08 20:29
刨子头
Rank: 1
等 级:新手上路
威 望:1
帖 子:319
专家分:0
注 册:2007-9-24
收藏
得分:0 
我顶!
2007-11-08 21:35
W11400661
Rank: 8Rank: 8
来 自:达拉达斯
等 级:蝙蝠侠
威 望:2
帖 子:163
专家分:834
注 册:2008-10-12
收藏
得分:0 
刚好用到!谢了!
2008-11-02 14:26
三断笛
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:31
帖 子:1621
专家分:1617
注 册:2007-5-24
收藏
得分:0 
这个东东以前见过 有些不好用 我改掉了很多地方 有空发上来
2008-11-02 20:03
快速回复:[原创]拿下注册表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.067365 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved