当前位置:开发者网络 >> 技术教程 >> .NET教程 >> 评论及其它 >> 内容
精彩推荐
分类最新教程
分类热点教程
  
开发手记(六)——共享软件注册程序编写实例(3)
作者:未知
日期:2005-09-16
人气:
投稿:(转贴)
来源:未知
字体:
收藏:加入浏览器收藏
以下正文:

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

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

(本篇完)
相关文章: