首先新建一個工程,在新建的窗體上添加以下控件並設置相應屬性:
控件名屬性值
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,看看右邊是不是生成了“注冊名”和“注冊公司”兩個鍵值,是的話,恭喜你,你成功了。->