分享
 
 
 

开发手记(六)——共享软件注册程序编写实例(3)

王朝other·作者佚名  2006-02-01
窄屏简体版  字體: |||超大  

开发手记(六)——共享软件注册程序编写实例(3)

-为保证文章完整性,谢绝对某篇而非全部手记的转载(mi6236)

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

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

(本篇完)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有