Now that we have the MyApp's startup form's .Top value saved in the Windows Registry, we need to access the value whenever our program starts up. We'll use the Visual Basic GetSetting function to read this value from the Windows Registry, and a good place to execute that function is in the Load Event Procedure of the form. Before I show you that code, let me show you the format for the GetSetting function.
Syntax
GetSetting(appname, section, key[, default])
The GetSetting function syntax has thesenamed arguments:
Part Description appnameRequired String expression containing the name of the application or project whose key setting is requested. sectionRequired String expression containing the name of the section where the key setting is found. key Required String expression containing the name of the key setting to return. defaultOptional Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").
As you can see, the first three arguments of the GetSetting function are identical to the SaveSetting statement. The difference lies in the fourth argument. With the SaveSetting Statement, the fourth argument was the value to be written to the Registry. For the GetSetting function, the fourth argument is an optional argument used to specify a default value to be returned in the event that the entry specified cannot be located in the Windows Registry.
Here's the code to place in the Load Event Procedure of the Startup Form. I have inserted 2 message box in the code below so that you can understand how it works...
To delete the entry that you made in the registry, you need to use DeleteSetting statement
Syntax
DeleteSetting appname, section[, key]
The DeleteSetting statement syntax has these named arguments:
Part Description appnameRequired String expression containing the name of the application or project to which the section or key setting applies. sectionRequired String expression containing the name of the section where the key setting is being deleted. If only appname and section are provided, the specified section is deleted along with all related key settings. keyOptional String expression containing the name of the key setting being deleted.
You can use it to delete the appname, section and key entry, similar to the way you created the entry to begin with. For example
vb Code:
DeleteSetting "MyApp", "Startup", "Top
After deleting the key, try searching the registry again and you will see that the key has been deleted.
Last edited by Siddharth Rout; Mar 26th, 2009 at 03:57 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Visual Basic provides us a function called GetAllSettings which can be used to return the Registry entries for a particular Appname and Setting. Here's the format for the GetAllSettings function. This is used for example in case we loose track of the entries we have made in the windows registry.
Syntax
GetAllSettings(appname, section)
The GetAllSettings function syntax has these named arguments:
Part Description appnameRequired String expression containing the name of the application orproject whose key settings are requested. sectionRequired String expression containing the name of the section whose key settings are requested. GetAllSettings returns aVariant whose contents is a two-dimensional array of strings containing all the key settings in the specified section and their corresponding values.
Once GetAllSettings executes and returns a two dimensional array, we can use the Lbound and Ubound functions, in conjunction with a For-Next Loop to move through the elements of the Array and display them as shown in the code below
Checking if a key created by SaveSetting exists using vb 6.0 code
Assuming that you haven't deleted the key that we created above, lets now try to find the key we created.
Like I mentioned above, Visual Basic, by default using GetSetting and SaveSetting allows reading and writing only to the VB and VBA Programs key under HKEY_Current_User\Software.
Create a form and place a Text Box and a Command Button in it. Place the code in the general declaration of the form.
vb Code:
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _
Long
Const KEY_READ = &H20019
Const HKEY_CURRENT_USER = &H80000001
'~~> Return True if a Registry key exists
Function CheckRegistryKey(ByVal hKey As Long, ByVal KeyName As String) As Boolean
Dim handle As Long
'~~> Try to open the key
If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) = 0 Then
'~~> The key exists
CheckRegistryKey = True
'~~> Close it before exiting
RegCloseKey handle
End If
End Function
Private Sub Command1_Click()
'~~> Visual Basic, by default using GetSetting and SaveSetting allows reading and
'~~> writing only to the VB and VBA Programs key under HKEY_Current_User\Software.
Run the Form and type "Software\VB and VBA Program Settings\MyApp" in the Text Box. Once done, click the Command Button. you will get the message "True" if the key exists or "False" if it doesn't.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Using API functions to programmatically find keys in the system registry. The sample demonstrates searches of root subkeys for specified text strings. An option to perform case-sensitive text search is also available.
The example uses the RegEnumKeyEx API function to enumerate subkeys of a specified open registry key. The function retrieves information about one subkey each time it is called.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Please note that all these API's have been extensively covered in http://allapi.mentalis.org/agnet/apiguide.shtml and hence I will not cover them here. However if you have any questions then refer to Summary in the next post...
RegCloseKey
The RegCloseKey function releases the handle of the specified key.
RegCreateKey
The RegCreateKey function creates the specified key. If the key already exists in the registry, the function opens it. This function is provided for compatibility with Windows version 3.1. Win32-based applications should use the RegCreateKeyEx function.
RegCreateKeyEx
The RegCreateKeyEx function creates the specified key. If the key already exists in the registry, the function opens it.
RegDeleteKey
Windows 95: The RegDeleteKey function deletes a key and all its descendents.
Windows NT: The RegDeleteKey function deletes the specified key. This function cannot delete a key that has subkeys.
RegDeleteValue
The RegDeleteValue function removes a named value from the specified registry key.
RegEnumKeyEx
The RegEnumKeyEx function enumerates subkeys of the specified open registry key. The function retrieves information about one subkey each time it is called.
RegEnumValue
The RegEnumValue function enumerates the values for the specified open registry key. The function copies one indexed value name and data block for the key each time it is called.
Edit: 18/06/2010
RegCreateKey, RegCloseKey and RegDeleteKey covered in this thread...