用VB5读写注册表实例
今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐用VB5读写注册表实例,希望大家看完后也有个好心情,快快行动吧!
控件名属性值
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)
Ifbufsize2Then
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)
Ifbufsize2Then
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,看看右边是不是生成了“注册名”和“注册公司”两个键值,是的话,恭喜你,你成功了。->