首页 > 开发 > 综合 > 正文

用API修改注册表的完整模块

2024-07-21 02:28:04
字体:
来源:转载
供稿:网友

有些老掉牙了,这是很久以前写的了,觉得功能已经比较完善了。

下载地址:
http://www.wowor.net/bbs/up/files/2004403_mregistry.zip



这是我用这个模块写过的一个软件:

注册表大师 2.0

http://www.onlinedown.net/soft/16780.htm



标准模块代码:
'**************************************************************************************************************
'* 本模块提供了一些对注册表进行操作的函数
'* 警告: 操作注册表是非常危险的, 使用本模块中的任何函数都要慎重!!!
'*
'* 版权: lpp软件工作室
'* 作者: 卢培培
'**************************************************************************************************************

option explicit

option compare text

'---------------------------------------------------------------
'- 注册表 api 声明...
'---------------------------------------------------------------
private declare function regclosekey lib "advapi32.dll" (byval hkey as long) as long
private declare function regcreatekeyex lib "advapi32.dll" 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, lpsecurityattributes as security_attributes, phkresult as long, lpdwdisposition as long) as long
private declare function regdeletekey lib "advapi32.dll" alias "regdeletekeya" (byval hkey as long, byval lpsubkey as string) as long
private declare function regdeletevalue lib "advapi32.dll" alias "regdeletevaluea" (byval hkey as long, byval lpvaluename as string) 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 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
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 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
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 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
private 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 filetime) as long

private declare function adjusttokenprivileges lib "advapi32.dll" (byval tokenhandle as long, byval disableallpriv as long, newstate as token_privileges, byval bufferlength as long, previousstate as token_privileges, returnlength as long) as long 'used to adjust your program's security privileges, can't restore without it!
private declare function lookupprivilegevalue lib "advapi32.dll" alias "lookupprivilegevaluea" (byval lpsystemname as any, byval lpname as string, lpluid as luid) as long 'returns a valid luid which is important when making security changes in nt.
private declare function openprocesstoken lib "advapi32.dll" (byval processhandle as long, byval desiredaccess as long, tokenhandle as long) as long
private declare function getcurrentprocess lib "kernel32" () as long

'---------------------------------------------------------------
'- 注册表 api 常数...
'---------------------------------------------------------------
' 注册表创建类型值...
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 error_none = 0
const error_badkey = 2
const error_access_denied = 8
const error_success = 0

' 有关导入/导出的常量
const reg_force_restore as long = 8&
const token_query as long = &h8&
const token_adjust_privileges as long = &h20&
const se_privilege_enabled as long = &h2
const se_restore_name = "serestoreprivilege"
const se_backup_name = "sebackupprivilege"

'---------------------------------------------------------------
'- 注册表类型...
'---------------------------------------------------------------
private type security_attributes
nlength as long
lpsecuritydescriptor as long
binherithandle as boolean
end type

private type filetime
dwlowdatetime as long
dwhighdatetime as long
end type

private type luid
lowpart as long
highpart as long
end type

private type luid_and_attributes
pluid as luid
attributes as long
end type

private type token_privileges
privilegecount as long
privileges as luid_and_attributes
end type

'---------------------------------------------------------------
'- 自定义枚举类型...
'---------------------------------------------------------------
' 注册表数据类型...
public enum valuetype
reg_sz = 1 ' 字符串值
reg_expand_sz = 2 ' 可扩充字符串值
reg_binary = 3 ' 二进制值
reg_dword = 4 ' dword值
reg_multi_sz = 7 ' 多字符串值
end enum

' 注册表关键字根类型...
public enum keyroot
hkey_classes_root = &h80000000
hkey_current_user = &h80000001
hkey_local_machine = &h80000002
hkey_users = &h80000003
hkey_performance_data = &h80000004
hkey_current_config = &h80000005
hkey_dyn_data = &h80000006
end enum

private hkey as long ' 注册表打开项的句柄
private i as long, j as long ' 循环变量
private success as long ' api函数的返回值, 判断函数调用是否成功

'-------------------------------------------------------------------------------------------------------------
'- 新建注册表关键字并设置注册表关键字的值...
'- 如果 valuename 和 value 都缺省, 则只新建 keyname 空项, 无子键...
'- 如果只缺省 valuename 则将设置指定 keyname 的默认值
'- 参数说明: keyroot--根类型, keyname--子项名称, valuename--值项名称, value--值项数据, valuetype--值项类型
'-------------------------------------------------------------------------------------------------------------
public function setkeyvalue(keyroot as keyroot, keyname as string, optional valuename as string, optional value as variant = "", optional valuetype as valuetype = reg_sz) as boolean
dim lpattr as security_attributes ' 注册表安全类型
lpattr.nlength = 50 ' 设置安全属性为缺省值...
lpattr.lpsecuritydescriptor = 0 ' ...
lpattr.binherithandle = true ' ...

' 新建注册表关键字...
success = regcreatekeyex(keyroot, keyname, 0, valuetype, reg_option_non_volatile, key_all_access, lpattr, hkey, 0)
if success <> error_success then setkeyvalue = false: regclosekey hkey: exit function

' 设置注册表关键字的值...
if ismissing(valuename) = false then
select case valuetype
case reg_sz, reg_expand_sz, reg_multi_sz
success = regsetvalueex(hkey, valuename, 0, valuetype, byval cstr(value), lenb(strconv(value, vbfromunicode)) + 1)
case reg_dword
if cdbl(value) <= 4294967295# and cdbl(value) >= 0 then
dim svalue as string
svalue = doubletohex(value)
dim dvalue(3) as byte
dvalue(0) = format("&h" & mid(svalue, 7, 2))
dvalue(1) = format("&h" & mid(svalue, 5, 2))
dvalue(2) = format("&h" & mid(svalue, 3, 2))
dvalue(3) = format("&h" & mid(svalue, 1, 2))
success = regsetvalueex(hkey, valuename, 0, valuetype, dvalue(0), 4)
else
success = error_badkey
end if
case reg_binary
on error resume next
success = 1 ' 假设调用api不成功(成功返回0)
redim tmpvalue(ubound(value)) as byte
for i = 0 to ubound(tmpvalue)
tmpvalue(i) = value(i)
next i
success = regsetvalueex(hkey, valuename, 0, valuetype, tmpvalue(0), ubound(value) + 1)
end select
end if
if success <> error_success then setkeyvalue = false: regclosekey hkey: exit function

' 关闭注册表关键字...
regclosekey hkey
setkeyvalue = true ' 返回函数值
end function

'-------------------------------------------------------------------------------------------------------------
'- 获得已存在的注册表关键字的值...
'- 如果 valuename="" 则返回 keyname 项的默认值...
'- 如果指定的注册表关键字不存在, 则返回空串...
'- 参数说明: keyroot--根类型, keyname--子项名称, valuename--值项名称, valuetype--值项类型
'-------------------------------------------------------------------------------------------------------------
public function getkeyvalue(keyroot as keyroot, keyname as string, valuename as string, optional valuetype as long) as string
dim tempvalue as string ' 注册表关键字的临时值
dim value as string ' 注册表关键字的值
dim valuesize as long ' 注册表关键字的值的实际长度
tempvalue = space(1024) ' 存储注册表关键字的临时值的缓冲区
valuesize = 1024 ' 设置注册表关键字的值的默认长度

' 打开一个已存在的注册表关键字...
regopenkeyex keyroot, keyname, 0, key_all_access, hkey

' 获得已打开的注册表关键字的值...
regqueryvalueex hkey, valuename, 0, valuetype, byval tempvalue, valuesize

' 返回注册表关键字的的值...
select case valuetype ' 通过判断关键字的类型, 进行处理
case reg_sz, reg_multi_sz, reg_expand_sz
tempvalue = left$(tempvalue, valuesize - 1) ' 去掉tempvalue尾部空格
value = tempvalue
case reg_dword
redim dvalue(3) as byte
regqueryvalueex hkey, valuename, 0, reg_dword, dvalue(0), valuesize
for i = 3 to 0 step -1
value = value + string(2 - len(hex(dvalue(i))), "0") + hex(dvalue(i)) ' 生成长度为8的十六进制字符串
next i
if cdbl("&h" & value) < 0 then ' 将十六进制的 value 转换为十进制
value = 2 ^ 32 + cdbl("&h" & value)
else
value = cdbl("&h" & value)
end if
case reg_binary
if valuesize > 0 then
redim bvalue(valuesize - 1) as byte ' 存储 reg_binary 值的临时数组
regqueryvalueex hkey, valuename, 0, reg_binary, bvalue(0), valuesize
for i = 0 to valuesize - 1
value = value + string(2 - len(hex(bvalue(i))), "0") + hex(bvalue(i)) + " " ' 将数组转换成字符串
next i
end if
end select

' 关闭注册表关键字...
regclosekey hkey
getkeyvalue = trim(value) ' 返回函数值
end function

'-------------------------------------------------------------------------------------------------------------
'- 删除已存在的注册表关键字的值...
'- 如果指定的注册表关键字不存在, 则不做任何操作...
'- 参数说明: keyroot--根类型, keyname--子项名称, valuename--值项名称
'-------------------------------------------------------------------------------------------------------------
public function deletekey(keyroot as keyroot, keyname as string, optional valuename as string) as boolean
dim tmpkeyname as string ' 注册表关键字的临时子项名称
dim tmpvaluename as string ' 注册表关键字的临时子键名称

' 打开一个已存在的注册表关键字...
success = regopenkeyex(keyroot, keyname, 0, key_all_access, hkey)
if success <> error_success then deletekey = false: regclosekey hkey: exit function

' 删除已打开的注册表关键字...
tmpkeyname = ""
tmpvaluename = keyname
if valuename = "" then ' 判断valuename是否缺省, 如缺省作相应处理
if instrrev(keyname, "/") > 1 then
tmpvaluename = right(keyname, instrrev(keyname, "/") + 1)
tmpkeyname = left(keyname, instrrev(keyname, "/") - 1)
end if
success = regopenkeyex(keyroot, tmpkeyname, 0, key_all_access, hkey)
success = regdeletekey(hkey, tmpvaluename)
else
success = regdeletevalue(hkey, valuename)
end if
if success <> error_success then deletekey = false: regclosekey hkey: exit function

' 关闭注册表关键字...
regclosekey hkey
deletekey = true ' 返回函数值
end function

'-------------------------------------------------------------------------------------------------------------
'- 获得注册表关键字的一些信息...
'- subkeyname() 注册表关键字的所有子项的名称(注意:最小下标为0)
'- valuename() 注册表关键字的所有子键的名称(注意:最小下标为0)
'- valuetype() 注册表关键字的所有子键的类型(注意:最小下标为0)
'- countkey 注册表关键字的子项数量
'- countvalue 注册表关键字的子键数量
'- maxlenkey 注册表关键字的子项名称的最大长度
'- maxlenvalue 注册表关键字的子键名称的最大长度
'-------------------------------------------------------------------------------------------------------------
public function getkeyinfo(keyroot as keyroot, keyname as string, subkeyname() as string, valuename() as string, valuetype() as valuetype, optional countkey as long, optional countvalue as long, optional maxlenkey as long, optional maxlenvalue as long) as boolean
dim f as filetime
dim l as long, s as string

' 打开一个已存在的注册表关键字...
success = regopenkeyex(keyroot, keyname, 0, key_all_access, hkey)
if success <> error_success then getkeyinfo = false: regclosekey hkey: exit function

' 获得一个已打开的注册表关键字的信息...
success = regqueryinfokey(hkey, vbnullstring, byval 0&, byval 0&, countkey, maxlenkey, byval 0&, countvalue, maxlenvalue, byval 0&, byval 0&, f)
if success <> error_success then getkeyinfo = false: regclosekey hkey: exit function

if countkey <> 0 then
redim subkeyname(countkey - 1) as string ' 重新定义数组, 使用数组大小与注册表关键字的子项数量匹配
for i = 0 to countkey - 1
subkeyname(i) = space(255)
l = 255
regenumkeyex hkey, i, byval subkeyname(i), l, 0, vbnullstring, byval 0&, f
subkeyname(i) = left(subkeyname(i), l)
next i

' 下面的二重循环对字符串数组进行冒泡排序
for i = 0 to ubound(subkeyname)
for j = i + 1 to ubound(subkeyname)
if subkeyname(i) > subkeyname(j) then
s = subkeyname(i)
subkeyname(i) = subkeyname(j)
subkeyname(j) = s
end if
next j
next i
end if

if countvalue <> 0 then
redim valuename(countvalue - 1) as string ' 重新定义数组, 使用数组大小与注册表关键字的子键数量匹配
redim valuetype(countvalue - 1) as long ' 重新定义数组, 使用数组大小与注册表关键字的子键数量匹配
for i = 0 to countvalue - 1
valuename(i) = space(255)
l = 255
regenumvalue hkey, i, byval valuename(i), l, 0, valuetype(i), byval 0&, byval 0&
valuename(i) = left(valuename(i), l)
next i

' 下面的二重循环对字符串数组进行冒泡排序
for i = 0 to ubound(valuename)
for j = i + 1 to ubound(valuename)
if valuename(i) > valuename(j) then
s = valuename(i)
valuename(i) = valuename(j)
valuename(j) = s
end if
next j
next i
end if

' 关闭注册表关键字...
regclosekey hkey
getkeyinfo = true ' 返回函数值
end function

'-------------------------------------------------------------------------------------------------------------
'- 导出注册表关键字的值
'- 参数说明: keyroot--根类型, keyname--子项名称, filename--导出的文件路径及文件名(原始数据库格式)
'-------------------------------------------------------------------------------------------------------------
public function savekey(keyroot as keyroot, keyname as string, filename as string) as boolean
on error resume next

dim lpattr as security_attributes ' 注册表安全类型
lpattr.nlength = 50 ' 设置安全属性为缺省值...
lpattr.lpsecuritydescriptor = 0 ' ...
lpattr.binherithandle = true ' ...

if enableprivilege(se_backup_name) = false then
savekey = false
exit function
end if

success = regopenkeyex(keyroot, keyname, 0&, key_all_access, hkey)
if success <> 0 then
savekey = false
success = regclosekey(hkey)
exit function
end if

success = regsavekey(hkey, filename, lpattr)
if success = 0 then savekey = true else savekey = false

success = regclosekey(hkey)
end function

'-------------------------------------------------------------------------------------------------------------
'- 导入注册表关键字的值
'- 参数说明: keyroot--根类型, keyname--子项名称, filename--导入的文件路径及文件名(原始数据库格式)
'-------------------------------------------------------------------------------------------------------------
public function restorekey(keyroot as keyroot, keyname as string, filename as string) as boolean
on error resume next

if enableprivilege(se_restore_name) = false then
restorekey = false
exit function
end if

success = regopenkeyex(keyroot, keyname, 0&, key_all_access, hkey)
if success <> 0 then
restorekey = false
success = regclosekey(hkey)
exit function
end if

success = regrestorekey(hkey, filename, reg_force_restore)
if success = 0 then restorekey = true else restorekey = false

success = regclosekey(hkey)
end function

'-------------------------------------------------------------------------------------------------------------
'- 使注册表允许导入/导出
'-------------------------------------------------------------------------------------------------------------
private function enableprivilege(sename as string) as boolean
on error resume next

dim p_lngrtn as long
dim p_lngtoken as long
dim p_lngbufferlen as long
dim p_typluid as luid
dim p_typtokenpriv as token_privileges
dim p_typprevtokenpriv as token_privileges

p_lngrtn = openprocesstoken(getcurrentprocess(), token_adjust_privileges or token_query, p_lngtoken)
if p_lngrtn = 0 then
enableprivilege = false
exit function
end if
if err.lastdllerror <> 0 then
enableprivilege = false
exit function
end if

p_lngrtn = lookupprivilegevalue(0&, sename, p_typluid)
if p_lngrtn = 0 then
enableprivilege = false
exit function
end if

p_typtokenpriv.privilegecount = 1
p_typtokenpriv.privileges.attributes = se_privilege_enabled
p_typtokenpriv.privileges.pluid = p_typluid

enableprivilege = (adjusttokenprivileges(p_lngtoken, false, p_typtokenpriv, len(p_typprevtokenpriv), p_typprevtokenpriv, p_lngbufferlen) <> 0)
end function

'-------------------------------------------------------------------------------------------------------------
'- 将 double 型( 限制在 0--2^32-1 )的数字转换为十六进制并在前面补零
'- 参数说明: number--要转换的 double 型数字
'-------------------------------------------------------------------------------------------------------------
private function doubletohex(byval number as double) as string
dim strhex as string
strhex = space(8)
for i = 1 to 8
select case number - int(number / 16) * 16
case 10
mid(strhex, 9 - i, 1) = "a"
case 11
mid(strhex, 9 - i, 1) = "b"
case 12
mid(strhex, 9 - i, 1) = "c"
case 13
mid(strhex, 9 - i, 1) = "d"
case 14
mid(strhex, 9 - i, 1) = "e"
case 15
mid(strhex, 9 - i, 1) = "f"
case else
mid(strhex, 9 - i, 1) = cstr(number - int(number / 16) * 16)
end select
number = int(number / 16)
next i
doubletohex = strhex
end function






发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表