欢迎光临
我们一直在努力

开发手记(六)——共享软件注册程序编写实例(3)-.NET教程,评论及其它

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

microsoft公司在windows nt3.51开始提出了注册表(registry)的概念。注册表是windows的一个重要组成部分,它保存了windows中的各种配置参数。windows的各个功能模块和安装的应用模块,在启动时都要读取注册表的信息,并根据这些参数来设置自己的运行环境,我们就可以把注册信息写入注册表来实现程序的共享注册。

在vb中,有两种方法可以访问注册表:第一种是使用vb提供的函数:

private sub command1_click()

dim keyname as string’创建的项目名称

dim subkeyname as string’项目下的子项

dim valuename as string’子项下的值项名称

dim value1 as string’值项的值

keyname=”mykey”

subkeyname=”mysubkey”

valuename=”myvaluename”

value1=”my new key!”

     ’查询是否存在myvaluename值项,如果不存在,返回noexit

ret=getsetting(keyname,subkeyname,valuename,”noexit”)

if ret=”noexit”then

   ’如果不存在myvaluename值项,则创建该值项

   savesetting keyname,subkeyname,valuename,value1

else

   ’如果存在,则删除这个程序项mykey

  deletesetting(keyname)

end if

end sub

创建的默认位置为hkey_current_user\software\vb and vba program settings下

第二种使用windows api函数处理注册表

global const hkey_classes_root=&h80000000

global const hkey_current_user=&h80000001

global const hkey_local_machine=&h80000002

global const hkey_users=&h80000003

global const error_success=0&

global const error_no_more_items=259&

    public const reg_sz=1

public const reg_binary=3

public const reg_dword=4

public const standard_rights_all=&h1f0000

public const key_query_value=&h1

public const key_set_value=&h2

public const key_create_sub_key=&h4

public const key_enumerate_sub_keys=&h8

public const key_notify=&h10

public const key_create_link=&h20

public const synchronize_ =&h100000

public const key_all_access=((standard_rights_all or key_query_value or key_set_value or key_create_sub_key or key_enumerate_sub_keys or key_notify or key_create_link) and (not synchronize))

public const reg_option_non_volatile=0

private declare function regcreatekey lib “advapi32.dll” alias “regcreatekeya” (byval hkey as long, byval lpsubkey as string, phkresult as long) 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 regclosekey lib “advapi32.dll” alias “regclosekey” (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 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 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 regdeletevalue lib “advapi32.dll” alias “regdeletevaluea” (byval hkey as long, byval lpvaluename as string) as long

’cmdccreatekey 创建项

’cmdcreatevalue 创建值项

’cmddeletevalue 删除项和值项

’cmdexit 退出

private sub cmdcreatekey_click()

    dim lresult as long

    dim hkeymykey as long

    dim dwflag as long

    dim msg, response

    lresult = regcreatekey(hkey_current_user, “software\mykey”, hkeymykey)

if lresult = error_success then

    msg = “成功创建项”

else

    msg = “创建项失败”

end if

        response = msgbox(msg, vbokonly, “提示信息”)

        regclosekey (hkeymykey)

end sub

private sub cmdcreatevalue_click()

    dim lresult as long

    dim hkeymykey as long

    dim szvalue as string

    dim msg, response

    打开注册表项

    lresult = regopenkeyex(hkey_current_user, “software\mykey”, 0, key_all_access, hkeymykey)

    if lresult = error_success then

        写入值项

        szvalue = “hello workd!”

        lresult = regsetvalueex(hkeymykey, “testvalue”, 0, reg_sz, byval szvalue, len(szvalue))

        msg = “成功创建值项testvalue!”

        关闭注册表项

        regclosekey (hkeymykey)

    else

        msg = “创建值项testvalue失败!”

end if

response=msgbox(msg,vbokonly)

end sub

private sub cmdqueryvalue_click()

    dim lresult as long

    dim msg, response

    lresult = regdeletekey(hkey_current_user, “software\mykey”)

    if lresult = error_success then

        msg = “成功删除项mykey!”

    else

        msg = “删除项mykey失败!”

    end if

    response = msgbox(msg, vbokonly)

endif

利用程序加密的安全性稍微低一些,如果您的软件价值很高的话,推荐使用加密狗(目前主流已经到了第三代),它的可靠性要高很多。如果您对加密解密非常感兴趣的话可以拜读一下段刚老大的《加密与解密》一书,或登陆看雪论坛交流。

(本篇完)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 开发手记(六)——共享软件注册程序编写实例(3)-.NET教程,评论及其它
分享到: 更多 (0)