您的位置:程序门 -> vb ->



如何读取注册表键值


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何读取注册表键值
发表于:2007-09-02 09:20:24 楼主
dim   name   as   string,   ret     as   long,   hkey   as   long
ret   =   regopenkey(hkey_current_user,   "software\microsoft\multimedia\sound   mapper ",   hkey)
if   ret   =   0   then
ret   =   regqueryvalueex(hkey,   "playback ",   0,   reg_sz,   byval   name,   255)
msgbox   name
end   if

请问以上代码有错吗?怎么读出来是空值?
发表于:2007-09-02 09:44:131楼 得分:0
'   这个模块用于读和写注册表关键字。
'   不同于vb   的内部注册表访问方法,它可以
'   通过字符串的值来读和写任何注册表关键字。


'---------------------------------------------------------------
'-注册表   api   声明...
'---------------------------------------------------------------
private   declare   function   regclosekey   lib   "advapi32 "   (byval   hkey   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,   byref   lpsecurityattributes   as   security_attributes,   byref   phkresult   as   long,   byref   lpdwdisposition   as   long)   as   long
private   declare   function   regopenkeyex   lib   "advapi32 "   alias   "regopenkeyexa "   (byval   hkey   as   long,   byval   lpsubkey   as   string,   byval   uloptions   as   long,   byval   samdesired   as   long,   byref   phkresult   as   long)   as   long
private   declare   function   regqueryvalueex   lib   "advapi32 "   alias   "regqueryvalueexa "   (byval   hkey   as   long,   byval   lpvaluename   as   string,   byval   lpreserved   as   long,   byref   lptype   as   long,   byval   lpdata   as   string,   byref   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   lpdata   as   string,   byval   cbdata   as   long)   as   long

'---------------------------------------------------------------
'-   注册表   api   常数...
'---------------------------------------------------------------
'   reg   data   types...
const   reg_sz   =   1                                                   '   unicode空终结字符串
const   reg_expand_sz   =   2                                     '   unicode空终结字符串
const   reg_dword   =   4                                             '   32-bit   数字

'   注册表创建类型值...
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   hkey_classes_root   =   &h80000000
const   hkey_current_user   =   &h80000001
const   hkey_local_machine   =   &h80000002
const   hkey_users   =   &h80000003
const   hkey_performance_data   =   &h80000004

'   返回值...
const   error_none   =   0
const   error_badkey   =   2
const   error_access_denied   =   8
const   error_success   =   0

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

发表于:2007-09-02 09:44:172楼 得分:0
'-------------------------------------------------------------------------------------------------
'sample   usage   -   debug.print   upodatekey(hkey_classes_root,   "keyname ",   "newvalue ")
'-------------------------------------------------------------------------------------------------
public   function   updatekey(keyroot   as   long,   keyname   as   string,   subkeyname   as   string,   subkeyvalue   as   string)   as   boolean
        dim   rc   as   long                                                                             '   返回代码
        dim   hkey   as   long                                                                         '   处理一个注册表关键字
        dim   hdepth   as   long                                                                     '
        dim   lpattr   as   security_attributes                                       '   注册表安全类型
       
        lpattr.nlength   =   50                                                                   '   设置安全属性为缺省值...
        lpattr.lpsecuritydescriptor   =   0                                           '   ...
        lpattr.binherithandle   =   true                                                 '   ...

        '------------------------------------------------------------
        '-   创建/打开注册表关键字...
        '------------------------------------------------------------
        rc   =   regcreatekeyex(keyroot,   keyname,   _
                                                0,   reg_sz,   _
                                                reg_option_non_volatile,   key_all_access,   lpattr,   _
                                                hkey,   hdepth)                                       '   创建/打开//keyroot//keyname
       
        if   (rc   <>   error_success)   then   goto   createkeyerror       '   错误处理...
       
        '------------------------------------------------------------
        '-   创建/修改关键字值...
        '------------------------------------------------------------
        if   (subkeyvalue   =   " ")   then   subkeyvalue   =   "   "                 '   要让regsetvalueex()   工作需要输入一个空格...
       
        '   创建/修改关键字值
        rc   =   regsetvalueex(hkey,   subkeyname,   _
                                              0,   reg_sz,   _
                                              subkeyvalue,   lenb(strconv(subkeyvalue,   vbfromunicode)))
                                             
        if   (rc   <>   error_success)   then   goto   createkeyerror       '   错误处理
        '------------------------------------------------------------
        '-   关闭注册表关键字...
        '------------------------------------------------------------
        rc   =   regclosekey(hkey)                                                             '   关闭关键字
       
        updatekey   =   true                                                                         '   返回成功
        exit   function                                                                               '   退出
createkeyerror:
        updatekey   =   false                                                                       '   设置错误返回代码
        rc   =   regclosekey(hkey)                                                             '   试图关闭关键字
end   function

'-------------------------------------------------------------------------------------------------
'sample   usage   -   debug.print   getkeyvalue(hkey_classes_root,   "comctl.listviewctrl.1\clsid ",   " ")
'-------------------------------------------------------------------------------------------------
public   function   getkeyvalue(keyroot   as   long,   keyname   as   string,   subkeyref   as   string)   as   string
        dim   i   as   long                                                                                       '   循环计数器
        dim   rc   as   long                                                                                     '   返回代码
        dim   hkey   as   long                                                                                 '   处理打开的注册表关键字
        dim   hdepth   as   long                                                                             '
        dim   skeyval   as   string
        dim   lkeyvaltype   as   long                                                                   '   注册表关键字数据类型
        dim   tmpval   as   string                                                                         '   注册表关键字的临时存储器
        dim   keyvalsize   as   long                                                                     '   注册表关键字变量尺寸
       
        '   在   keyroot   {hkey_local_machine...}   下打开注册表关键字
        '------------------------------------------------------------
        rc   =   regopenkeyex(keyroot,   keyname,   0,   key_all_access,   hkey)   '   打开注册表关键字
       
        if   (rc   <>   error_success)   then   goto   getkeyerror                     '   处理错误...
       
        tmpval   =   string$(1024,   0)                                                           '   分配变量空间
        keyvalsize   =   1024                                                                               '   标记变量尺寸
       
        '------------------------------------------------------------
        '   检索注册表关键字的值...
        '------------------------------------------------------------
        rc   =   regqueryvalueex(hkey,   subkeyref,   0,   _
                                                  lkeyvaltype,   tmpval,   keyvalsize)         '   获得/创建关键字的值
                                               
        if   (rc   <>   error_success)   then   goto   getkeyerror                     '   错误处理
           
        tmpval   =   left$(tmpval,   instr(tmpval,   chr(0))   -   1)

        '------------------------------------------------------------
        '   决定关键字值的转换类型...
        '------------------------------------------------------------
        select   case   lkeyvaltype                                                                     '   搜索数据类型...
        case   reg_sz,   reg_expand_sz                                                             '   字符串注册表关键字数据类型
                skeyval   =   tmpval                                                                           '   复制字符串的值
        case   reg_dword                                                                                     '   四字节注册表关键字数据类型
                for   i   =   len(tmpval)   to   1   step   -1                                         '   转换每一位
                        skeyval   =   skeyval   +   hex(asc(mid(tmpval,   i,   1)))       '   一个字符一个字符地生成值。
                next
                skeyval   =   format$( "&h "   +   skeyval)                                           '   转换四字节为字符串
        end   select
       
        getkeyvalue   =   skeyval                                                                       '   返回值
        rc   =   regclosekey(hkey)                                                                     '   关闭注册表关键字
        exit   function                                                                                       '   退出
       
getkeyerror:         '   错误发生过后进行清除...
        getkeyvalue   =   vbnullstring                                                             '   设置返回值为空字符串
        rc   =   regclosekey(hkey)                                                                     '   关闭注册表关键字
end   function

发表于:2007-09-02 11:09:063楼 得分:0
晕...发一堆这东西给我干嘛....问题我不知道哪里错了....-   -
发表于:2007-09-03 13:32:124楼 得分:0
你的错误:      

    请来一个工程师,却不给别人住的地方:)

ret   =   regqueryvalueex(hkey,   "playback ",   0,   reg_sz,   byval   name,   255)

这一句之前,再加一句:

name   =   space(255)

至于hotus发的代码,那可是好东西呀,你怎么一点都不领情:)
发表于:2007-09-04 08:39:475楼 得分:0
调用   api,一定要给字符串分配内存。你这里出空值还是好的,有些情况下会程序崩溃。

方法有二:
1
dim   name   as   string   *   255
dim   ret     as   long,   hkey   as   long
ret   =   regopenkey(hkey_current_user,   "software\microsoft\multimedia\sound   mapper ",   hkey)

2
dim   name   as   string,   ret     as   long,   hkey   as   long
name   =   string(255,   chr(0))   '或
'name   =   space(255)
ret   =   regopenkey(hkey_current_user,   "software\microsoft\multimedia\sound   mapper ",   hkey)
发表于:2007-09-04 10:21:526楼 得分:0
mark


快速检索

最新资讯
热门点击