Use VB5 read and write registry instances

zhaozj2021-02-11  182

Use the VB5 Read Write Registry Instance First, add the following control, add the following controls on the new form and set the appropriate attribute: control name property value Label1 CAPTION registry key value: Label2 CAPTION Name: Label3 CAPTION Empty Label4 CAPTION Registration Company: Label5 Caption empty Label6 Caption Text1 Text empty empty empty Text2 Text Command1 Caption write to the registry Command2 Caption exit and then add the following code in the declaration part of the program: Private declare Function RegCreateKey Lib "advapi32.dll" Alias ​​"RegCreateKeyA" (ByVal hKey as Long, ByVal lpSubKey As String, phkResult As Long) As LongPrivate 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 LongPrivate 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 Longconst Error_Success = 0 & const error_baddb = 1009 & const error_badkey = 1010 & const error_cantopen = 1011 & const error_cantread = 1012 & const error_cantwrite = 1013 & const err or_registry_recovered = 1014 & Const error_registry_corrupt = 1015 & Const error_registry_io_failed = 1016 & Const hkey_classes_root = & H80000000Const hkey_current_user = & H80000001Const hkey_local_machine = & H80000002Const REG_SZ = 1Const regkey = "software / My Soft / My program" was then added to each control code, the code is as follows: Private Sub Command1_Click () Dim keyvalue As Stringdim RetValue As Longdim Keyid As LongretValue = RegcreateKey (H DA0 Key_Local_Machine, RegKey, Keyid) KeyValue = Text1.TextRetValue = RegSetValueex (KeyID, "Registered Name", 0 &

, REG_SZ, BYVAL KeyValue, Len (KeyValue) 1) KeyValue = Text2.TextRetValue = RegSetValueex (KeyID, "Registered Company", 0 &, Reg_SZ, Byval KeyValue, Len (KeyValue) 1) If Text1.Text <> " And Text2.Text <> "" ThenLabel3.Caption = Text1.TextLabel5.Caption = Text2.TextEnd IfEnd SubPrivate Sub Command2_Click () Unload MeEndEnd SubPrivate Sub Form_Load () Dim retvalue As LongDim result As LongDim keyid As LongDim keyvalue As StringDim subkey As StringDim bufsize As LongLabel6.Caption = regkeyretvalue = RegCreateKey (hkey_local_machine, regkey, keyid) If retvalue = 0 Thensubkey = "registered name" retvalue = RegQueryValueEx (keyid, subkey, 0 &, REG_SZ, 0 &, bufsize) If bufsize <2 Thenkeyvalue = "" retvalue = RegSetValueEx (keyid, subkey, 0 &, REG_SZ, ByVal keyvalue, Len (keyvalue) 1) Elsekeyvalue = String (bufsize 1, "") retvalue = RegQueryValueEx (keyid, subkey, 0 &, REG_SZ, ByVal keyvalue, bufsize) KeyValue = Left $ (KeyValue, Bufsize - 1) text1.text = keyvalueend iflabel3.caption = keyvalueSubkey = "Register" RetValue = RegQueryValueex (ke yid, subkey, 0 &, REG_SZ, 0 &, bufsize) If bufsize <2 Thenkeyvalue = "" retvalue = RegSetValueEx (keyid, subkey, 0 &, REG_SZ, ByVal keyvalue, Len (keyvalue) 1) Elsekeyvalue = String (bufsize 1, "") retvalue = RegQueryValueEx (keyid, subkey, 0 &, REG_SZ, ByVal keyvalue, bufsize) keyvalue = Left $ (keyvalue, bufsize - 1) Text2.Text = keyvalueEnd IfLabel5.Caption = keyvalueEnd IfEnd Sub well, save the file, press F5 Run the program, write your "Registered Name" and "Registered Company" in two text, and then click the "Write Registry" button, now open the Registry Editor,

转载请注明原文地址:https://www.9cbs.com/read-5719.html

New Post(0)