用VB5读写注册表实例
2006-02-27 21:15:35 来源:WEB开发网核心提示:首先新建一个工程,在新建的窗体上添加以下控件并设置相应属性:控件名属性值Label1Caption注册表键值:Label2Caption注册姓名:Label3Caption空Label4Caption注册公司:Label5Caption空Label6Caption空Text1Text空Text2Text空Command
首先新建一个工程,在新建的窗体上添加以下控件并设置相应属性:
控件名属性值
Label1Caption注册表键值:
Label2Caption注册姓名:
Label3Caption空
Label4Caption注册公司:
Label5Caption空
Label6Caption空
Text1Text空
Text2Text空
Command1Caption写入注册表
Command2Caption退出
然后,在程序的声明部分添加如下代码:
PRivateDeclareFunctionRegCreateKeyLib"advapi32.dll"Alias"RegCreateKeyA"(ByValhKeyAsLong,ByVallpSubKeyAsString,phkResultAsLong)AsLong
PrivateDeclareFunctionRegQueryValueExLib"advapi32.dll"Alias"RegQueryValueExA"(ByValhKeyAsLong,ByVallpValueNameAsString,ByVallpReservedAsLong,lpTypeAsLong,lpDataAsAny,lpcbDataAsLong)AsLong
PrivateDeclareFunctionRegSetValueExLib"advapi32.dll"Alias"RegSetValueExA"(ByValhKeyAsLong,ByVallpValueNameAsString,ByValReservedAsLong,ByValdwTypeAsLong,lpDataAsAny,ByValcbDataAsLong)AsLong
Consterror_success=0&
Consterror_baddb=1009&
Consterror_badkey=1010&
Consterror_cantopen=1011&
Consterror_cantread=1012&
Consterror_cantwrite=1013&
Consterror_registry_recovered=1014&
Consterror_registry_corrupt=1015&
Consterror_registry_io_failed=1016&
Consthkey_classes_root=&H80000000
Consthkey_current_user=&H80000001
Consthkey_local_machine=&H80000002
ConstREG_SZ=1
Constregkey="softwareMySoftMyprogram"
接着为各个控件添加代码,代码如下:
PrivateSubCommand1_Click()
DimkeyvalueAsString
DimretvalueAsLong
DimkeyidAsLong
retvalue=RegCreateKey(hda0key_local_machine,regkey,keyid)
keyvalue=Text1.Text
retvalue=RegSetValueEx(keyid,"注册名",0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
keyvalue=Text2.Text
retvalue=RegSetValueEx(keyid,"注册公司",0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
IfText1.Text<>""AndText2.Text<>""Then
Label3.Caption=Text1.Text
Label5.Caption=Text2.Text
EndIf
EndSub
PrivateSubCommand2_Click()
UnloadMe
End
EndSub
PrivateSubForm_Load()
DimretvalueAsLong
DimresultAsLong
DimkeyidAsLong
DimkeyvalueAsString
DimsubkeyAsString
DimbufsizeAsLong
Label6.Caption=regkey
retvalue=RegCreateKey(hkey_local_machine,regkey,keyid)
Ifretvalue=0Then
subkey="注册名"
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,0&,bufsize)
Ifbufsize<2Then
keyvalue=""
retvalue=RegSetValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
Else
keyvalue=String(bufsize 1,"")
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,bufsize)
keyvalue=Left$(keyvalue,bufsize-1)
Text1.Text=keyvalue
EndIf
Label3.Caption=keyvalue
subkey="注册公司"
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,0&,bufsize)
Ifbufsize<2Then
keyvalue=""
retvalue=RegSetValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
Else
keyvalue=String(bufsize 1,"")
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,bufsize)
keyvalue=Left$(keyvalue,bufsize-1)
Text2.Text=keyvalue
EndIf
Label5.Caption=keyvalue
EndIf
EndSub
好了,存盘,按F5运行程序,在两个Text中分别写入你的“注册姓名”和“注册公司”的名称,然后单击
“写入注册表”按钮,现在打开“注册表编辑器”,找到HKEY_LOCAL_MACHINESOFTWAREMySoftMyProgram,看看右边是不是生成了“注册名”和“注册公司”两个键值,是的话,恭喜你,你成功了。->
控件名属性值
Label1Caption注册表键值:
Label2Caption注册姓名:
Label3Caption空
Label4Caption注册公司:
Label5Caption空
Label6Caption空
Text1Text空
Text2Text空
Command1Caption写入注册表
Command2Caption退出
然后,在程序的声明部分添加如下代码:
PRivateDeclareFunctionRegCreateKeyLib"advapi32.dll"Alias"RegCreateKeyA"(ByValhKeyAsLong,ByVallpSubKeyAsString,phkResultAsLong)AsLong
PrivateDeclareFunctionRegQueryValueExLib"advapi32.dll"Alias"RegQueryValueExA"(ByValhKeyAsLong,ByVallpValueNameAsString,ByVallpReservedAsLong,lpTypeAsLong,lpDataAsAny,lpcbDataAsLong)AsLong
PrivateDeclareFunctionRegSetValueExLib"advapi32.dll"Alias"RegSetValueExA"(ByValhKeyAsLong,ByVallpValueNameAsString,ByValReservedAsLong,ByValdwTypeAsLong,lpDataAsAny,ByValcbDataAsLong)AsLong
Consterror_success=0&
Consterror_baddb=1009&
Consterror_badkey=1010&
Consterror_cantopen=1011&
Consterror_cantread=1012&
Consterror_cantwrite=1013&
Consterror_registry_recovered=1014&
Consterror_registry_corrupt=1015&
Consterror_registry_io_failed=1016&
Consthkey_classes_root=&H80000000
Consthkey_current_user=&H80000001
Consthkey_local_machine=&H80000002
ConstREG_SZ=1
Constregkey="softwareMySoftMyprogram"
接着为各个控件添加代码,代码如下:
PrivateSubCommand1_Click()
DimkeyvalueAsString
DimretvalueAsLong
DimkeyidAsLong
retvalue=RegCreateKey(hda0key_local_machine,regkey,keyid)
keyvalue=Text1.Text
retvalue=RegSetValueEx(keyid,"注册名",0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
keyvalue=Text2.Text
retvalue=RegSetValueEx(keyid,"注册公司",0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
IfText1.Text<>""AndText2.Text<>""Then
Label3.Caption=Text1.Text
Label5.Caption=Text2.Text
EndIf
EndSub
PrivateSubCommand2_Click()
UnloadMe
End
EndSub
PrivateSubForm_Load()
DimretvalueAsLong
DimresultAsLong
DimkeyidAsLong
DimkeyvalueAsString
DimsubkeyAsString
DimbufsizeAsLong
Label6.Caption=regkey
retvalue=RegCreateKey(hkey_local_machine,regkey,keyid)
Ifretvalue=0Then
subkey="注册名"
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,0&,bufsize)
Ifbufsize<2Then
keyvalue=""
retvalue=RegSetValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
Else
keyvalue=String(bufsize 1,"")
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,bufsize)
keyvalue=Left$(keyvalue,bufsize-1)
Text1.Text=keyvalue
EndIf
Label3.Caption=keyvalue
subkey="注册公司"
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,0&,bufsize)
Ifbufsize<2Then
keyvalue=""
retvalue=RegSetValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,Len(keyvalue) 1)
Else
keyvalue=String(bufsize 1,"")
retvalue=RegQueryValueEx(keyid,subkey,0&,REG_SZ,ByValkeyvalue,bufsize)
keyvalue=Left$(keyvalue,bufsize-1)
Text2.Text=keyvalue
EndIf
Label5.Caption=keyvalue
EndIf
EndSub
好了,存盘,按F5运行程序,在两个Text中分别写入你的“注册姓名”和“注册公司”的名称,然后单击
“写入注册表”按钮,现在打开“注册表编辑器”,找到HKEY_LOCAL_MACHINESOFTWAREMySoftMyProgram,看看右边是不是生成了“注册名”和“注册公司”两个键值,是的话,恭喜你,你成功了。->
[]
更多精彩
赞助商链接