一、常數定義和API申明(我們會在使用該API時對它進行說明): PrivateConstHKEY_CLASSES_ROOT=&H80000000主鍵
PrivateConstHKEY_CURRENT_USER=&H80000001主鍵
PrivateConstSTANDARD_RIGHTS_ALL=&H1F0000
PrivateConstKEY_ENUMERATE_SUB_KEYS=&H8
PrivateConstKEY_SET_VALUE=&H2
PrivateConstKEY_CREATE_SUB_KEY=&H4
PrivateConstKEY_QUERY_VALUE=&H1
PrivateConstKEY_NOTIFY=&H10
PrivateConstKEY_CREATE_LINK=&H20
PrivateConstSYNCHRONIZE=&H100000
PrivateConstKEY_ALL_ACCESS=((STANDARD_RIGHTS_ALLOrKEY_QUERY_VALUEOrKEY_SET_VALUEOrKEY_CREATE_SUB_KEYOrKEY_ENUMERATE_SUB_KEYSOrKEY_NOTIFYOrKEY_CREATE_LINK)And(NotSYNCHRONIZE))
PrivateConstREG_MULTI_SZ=7
PrivateConstERROR_SUCCESS=0&
PrivateConstREAD_CONTROL=&H20000
PrivateConstREG_SZ=1
PrivateConstREG_DWORD=4
PrivateConstREG_EXPAND_SZ=2
PrivateConstREG_BINARY=3
PrivateConstREG_DWORD_BIG_ENDIAN=5
PrivateConstREG_DWORD_LITTLE_ENDIAN=4
PrivateConstREG_NONE=0
PrivateDeclareFunctionRegOpenKeyExLib“advapi32.dll"Alias“RegOpenKeyExA"(ByValhKeyAsLong,ByVallpSubKeyAsString,ByValulOptionsAsLong,ByValsamDesiredAsLong,phkResultAsLong)AsLong
PrivateDeclareFunctionRegCreateKeyLib“advapi32.dll"Alias“RegCreateKeyA"(ByValhKeyAsLong,ByVallpSubKeyAsString,phkResultAsLong)AsLong
PrivateDeclareFunctionRegSetValueExLib“advapi32.dll"Alias“RegSetValueExA"(ByValhKeyAsLong,ByVallpValueNameAsString,ByValReservedAsLong,ByValdwTypeAsLong,lpDataAsAny,ByValcbDataAsLong)AsLong
PrivateDeclareFunctionRegQueryValueExLib"advapi32.dll"Alias"RegQueryValueExA"(ByValhKeyAsLong,ByVallpValueNameAsString,ByVallpReservedAsLong,lpTypeAsLong,lpDataAs
Any,lpcbDataAsLong)AsLongPrivateDeclareFunctionRegCloseKeyLib"advapi32.dll"(ByValhKeyAsLong)AsLong
二、將下列代碼放在窗體的Load事件中
PrivateSubForm_Load()
DimhKeyAsLong主鍵的句柄
DimlpDataAsString查詢得到的鍵值
DimSizeOfDataAsLong鍵值的長度
DimValueTypeAsLong鍵值的類型
SizeOfData=150
Dimreturn_OpenKeyAsLong函數調用的返回值
DimPriKeyAsString主鍵
DimKeyValue1,KeyValue2AsString鍵值名稱
PriKey="北信BITIPeople"主鍵
KeyValue1="教授"鍵值名稱
KeyValue2="學生"鍵值名稱
下面的函數RegOpenKeyEx取得一個句柄hKey
return_OpenKey=RegOpenKeyEx(HKEY_CURRENT_USER,"",0,KEY_ALL_ACCESS,hKey)
下面的函數RegCreateKey建立一個主鍵,如此鍵已存在,則打開它IfRegCreateKey(hKey,PriKey,hKey)<>ERROR_SUCCESSThen
MsgBox"create"&PriKey&"Failed"
ExitSub
EndIf
DimKeyDataAsString鍵值
KeyData="500"
下面的函數RegSetValueEx給指定的鍵值名稱賦鍵值
IfRegSetValueEx(hKey,KeyValue1,0&,REG_SZ,ByValKeyData,Len(KeyData)+1)<>ERROR_SUCCESSThen
MsgBox"SetValue"&KeyValue1&"Failed"
ExitSub
EndIf
KeyData="15020"
下面的函數RegSetValueEx給指定的鍵值名稱"學生"賦鍵值為"15020"
IfRegSetValueEx(hKey,KeyValue2,0&,REG_SZ,ByValKeyData,Len(KeyData)+1)<>ERROR_SUCCESSThen
MsgBox"SetValue"&KeyValue2&"Failed"
ExitSub
EndIf
return_OpenKey=RegQueryValueEx(hKey,KeyValue1,0&,REG_SZ,0&,SizeOfData)
lpData=String(SizeOfData+1,"")
下面的函數RegQueryValueEx檢索指定鍵值名稱的鍵值和鍵值類型
IfRegQueryValueEx(hKey,KeyValue1,0&,REG_SZ,ByVallpData,SizeOfData)<>ERROR_SUCCESSThen
MsgBox"Query"&KeyValue1&"Failed"
EndIf
RegCloseKey(hKey)釋放主鍵的句柄
EndSub
運行此程序後,打開注冊表,我們會發現在主鍵HKEY_CURRENT_USER(在此主鍵下記錄本機當前用戶的一些信息)下,增加了子鍵"北信BITIPeople",我們還可看到兩個指定的鍵值名稱都有正確的鍵值。從而完成了注冊的任務。
注:此程序在VB5.0中運行通過。
->