欢迎光临
我们一直在努力

一个好用的 VB 注册表操作类模块-.NET教程,VB.Net语言

建站超值云服务器,限时71元/月

 

具体使用一看就明白了,来自 csdn

option explicit

private declare sub copymemory lib “kernel32” alias “rtlmovememory” _
   (dest as any, source as any, byval numbytes as long)

private declare function expandenvironmentstrings lib “kernel32” alias “expandenvironmentstringsa” _
   (byval lpsrc as string, byval lpdst as string, byval nsize as long) as long
  
//注册表 api 函数声明
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 regclosekey lib “advapi32.dll” (byval hkey 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 regsetvalueex lib “advapi32” alias “regsetvalueexa” _
   (byval hkey as long, byval lpvaluename as string, _
   byval reserved as long, byval dwtype as long, _
   byval lpbdata as any, byval cbdata as long) as long

private declare function regcreatekeyex lib “advapi32” 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 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 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, byval lpdata as string, lpcbdata 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 regsetvalueexstring lib “advapi32.dll” alias “regsetvalueexa” _
   (byval hkey as long, byval ipvaluename as string, _
   byval reserved as long, byval dwtype as long, _
   byval lpvalue as string, byval cbdata as long) as long

private 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

private declare function regsetvalueexbyte lib “advapi32.dll” alias “regsetvalueexa” _
   (byval hkey as long, byval lpvaluename as string, _
   byval reserved as long, byval dwtype as long, _
   lpvalue as byte, 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 regenumvalueint 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 regenumvaluestr 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 regenumvaluebyte 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 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

//注册表访问权
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_all_access = &h3f

//打开/建立选项
const reg_option_non_volatile = 0&
const reg_option_volatile = &h1

//key 创建/打开
const reg_created_new_key = &h1
const reg_opened_existing_key = &h2

//预定义存取类型
const standard_rights_all = &h1f0000
const specific_rights_all = &hffff

//严格代码定义
const error_success = 0&
const error_access_denied = 5
const error_no_more_items = 259
const error_more_data = 234 //  错误

//注册表值类型列举
private enum regdatatypeenum
   reg_none = (0)                         // no value type
   reg_sz = (1)                           // unicode nul terminated string
   reg_expand_sz = (2)                    // unicode nul terminated string w/enviornment var
   reg_binary = (3)                       // free form binary
   reg_dword = (4)                        // 32-bit number
   reg_dword_little_endian = (4)          // 32-bit number (same as reg_dword)
   reg_dword_big_endian = (5)             // 32-bit number
   reg_link = (6)                         // symbolic link (unicode)
   reg_multi_sz = (7)                     // multiple, null-delimited, double-null-terminated unicode strings
   reg_resource_list = (8)                // resource list in the resource map
   reg_full_resource_descriptor = (9)     // resource list in the hardware description
   reg_resource_requirements_list = (10)
end enum
  
//注册表基本键值列表
public enum rootkeyenum
   hkey_classes_root = &h80000000
   hkey_current_user = &h80000001
   hkey_local_machine = &h80000002
   hkey_users = &h80000003
   hkey_performance_data_win2k_only = &h80000004 //仅win2k
   hkey_current_config = &h80000005
   hkey_dyn_data = &h80000006
end enum

// for specifying the type of data to save
public enum regvaluetypes
   einteger = vbinteger
   elong = vblong
   estring = vbstring
   ebytearray = vbarray + vbbyte
end enum

//保存时指定类型
public enum regflags
   isexpandablestring = 1
   ismultistring = 2
   isbigendian = 3 // 无指针同样不要设置大endian值
end enum

private const err_none = 0

function setregistryvalue(byval hkey as rootkeyenum, byval keyname as string, _
   byval valuename as string, byval value as variant, valuetype as regvaluetypes, _
   optional flag as regflags = 0) as boolean
  
   dim handle as long
   dim lngvalue as long
   dim strvalue as string
   dim binvalue() as byte
   dim length as long
   dim retval as long
  
   dim secattr as security_attributes //键的安全设置
   //设置新键值的名称和默认安全设置
   secattr.nlength = len(secattr) //结构大小
   secattr.lpsecuritydescriptor = 0 //默认安全权限
   secattr.binherithandle = true //设置的默认值

   // 打开或创建键
   if regopenkeyex(hkey, keyname, 0, key_all_access, handle) then exit function
   retval = regcreatekeyex(hkey, keyname, 0, vbnullstring, reg_option_non_volatile, key_all_access, secattr, handle, retval)
   if retval then exit function

   //3种数据类型
   select case vartype(value)
      case vbbyte, vbinteger, vblong // 若是字节, integer值或long值…
         lngvalue = value
         retval = regsetvalueexlong(handle, valuename, 0, reg_dword, lngvalue, len(lngvalue))
     
      case vbstring // 字符串, 扩展环境字符串或多段字符串…
         strvalue = value
         select case flag
            case isexpandablestring
               retval = regsetvalueex(handle, valuename, 0, reg_expand_sz, byval strvalue, 255)
            case ismultistring
               retval = regsetvalueex(handle, valuename, 0, reg_multi_sz, byval strvalue, 255)
            case else // 正常 reg_sz 字符串
               retval = regsetvalueex(handle, valuename, 0, reg_sz, byval strvalue, 255)
         end select
     
      case vbarray + vbbyte // 如果是字节数组…
         binvalue = value
         length = ubound(binvalue) – lbound(binvalue) + 1
         retval = regsetvalueexbyte(handle, valuename, 0, reg_binary, binvalue(0), length)
     
      case else // 如果其它类型
         regclosekey handle
         err.raise 1001, , “不支持的值类型”
  
   end select

   // 返回关闭结果
   regclosekey handle
  
   // 返回写入成功结果
   setregistryvalue = (retval = 0)

end function

function getregistryvalue(byval hkey as rootkeyenum, byval keyname as string, _
   byval valuename as string, optional defaultvalue as variant) as variant
  
   dim handle as long
   dim reslong as long
   dim resstring as string
   dim resbinary() as byte
   dim length as long
   dim retval as long
   dim valuetype as long

   const key_read = &h20019
  
   // 默认结果
   getregistryvalue = iif(ismissing(defaultvalue), empty, defaultvalue)
  
   // 打开键, 不存在则退出
   if regopenkeyex(hkey, keyname, 0, key_read, handle) then exit function
  
   // 准备 1k  resbinary 用于接收
   length = 1024
   redim resbinary(0 to length – 1) as byte
  
   // 读注册表值
   retval = regqueryvalueex(handle, valuename, 0, valuetype, resbinary(0), length)
  
   // 若resbinary 太小则重读
   if retval = error_more_data then
      // resbinary放大,且重新读取
      redim resbinary(0 to length – 1) as byte
      retval = regqueryvalueex(handle, valuename, 0, valuetype, resbinary(0), _
      length)
   end if
  
   // 返回相应值类型
   select case valuetype
      case reg_dword, reg_dword_little_endian
         // reg_dword 和 reg_dword_little_endian 相同
         copymemory reslong, resbinary(0), 4
         getregistryvalue = reslong
     
      case reg_dword_big_endian
         // big endians 用在非-windows环境, 如unix系统, 本地计算机远程访问
         copymemory reslong, resbinary(0), 4
         getregistryvalue = swapendian(reslong)
     
      case reg_sz, reg_expand_sz
         resstring = space$(length – 1)
         copymemory byval resstring, resbinary(0), length – 1
         if valuetype = reg_expand_sz then
            // 查询对应的环境变量
            getregistryvalue = expandenvstr(resstring)
         else
            getregistryvalue = resstring
         end if

      case reg_multi_sz
         // 复制时需指定2个空格符
         resstring = space$(length – 2)
         copymemory byval resstring, resbinary(0), length – 2
         getregistryvalue = resstring

      case else 包含 reg_binary
         // resbinary 调整
         if length <> ubound(resbinary) + 1 then
            redim preserve resbinary(0 to length – 1) as byte
         end if
      getregistryvalue = resbinary()
  
   end select
  
   // 关闭
   regclosekey handle

end function

public function deleteregistryvalueorkey(byval hkey as rootkeyenum, regkeyname as string, _
   valuename as string) as boolean
//删除注册表值和键,如果成功返回true

   dim lretval as long      //打开和输出注册表键的返回值
   dim lreghwnd as long     //打开注册表键的句柄
   dim sregszdata as string //把获取值放入缓冲区
   dim lslength as long     //缓冲区大小.  改变缓冲区大小要在调用之后
  
   //打开键
   lretval = regopenkeyex(hkey, regkeyname, 0, key_all_access, lreghwnd)
  
   //成功打开
   if lretval = err_none then
      //删除指定值
      lretval = regdeletevalue(lreghwnd, valuename)  //如果已存在则先删除
     
      //如出现错误则删除值并返回false
      if lretval <> err_none then exit function
     
      //注意: 如果成功打开仅关闭注册表键
      lretval = regclosekey(lreghwnd)
    
      //如成功关闭则返回 true 或者其它错误
      if lretval = err_none then deleteregistryvalueorkey = true
     
   end if

end function

private function expandenvstr(sdata as string) as string
// 查询环境变量和返回定义值
// 如: %path% 则返回 “c:\;c:\windows;”

   dim c as long, s as string
  
   s = “” // 不支持windows 95
  
   // get the length
   c = expandenvironmentstrings(sdata, s, c)
  
   // 展开字符串
   s = string$(c – 1, 0)
   c = expandenvironmentstrings(sdata, s, c)
  
   // 返回环境变量
   expandenvstr = s
  
end function

private function swapendian(byval dw as long) as long
// 转换大dword 到小 dword
  
   copymemory byval varptr(swapendian) + 3, dw, 1
   copymemory byval varptr(swapendian) + 2, byval varptr(dw) + 1, 1
   copymemory byval varptr(swapendian) + 1, byval varptr(dw) + 2, 1
   copymemory swapendian, byval varptr(dw) + 3, 1

end function

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个好用的 VB 注册表操作类模块-.NET教程,VB.Net语言
分享到: 更多 (0)