Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Platforms: Win 32s, Win 95/98, Win NT
RegCloseKey closes a registry key. This should be done after you finish reading to or writing to the registry. Closing the registry key frees up some resources. Obviously, you can no longer use the key after closing it. The function returns 0 if successful, or a non-zero error code if an error occurs.
Example:
' Create a key called HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config.
' Then create a "username" value under that key and set its value to "Rimmer".
Dim hregkey As Long ' receives handle to the newly created or opened registry key
Dim secattr As SECURITY_ATTRIBUTES ' security settings of the key
Dim subkey As String ' name of the subkey to create
Dim neworused As Long ' receives 1 if new key was created or 2 if an existing key was opened
Dim stringbuffer As String ' the string to put into the registry
Dim retval As Long ' return value
' Set the name of the new key and the default security settings
subkey = "Software\MyCorp\MyProgram\Config"
secattr.nLength = Len(secattr) ' size of the structure
secattr.lpSecurityDescriptor = 0 ' default security level
secattr.bInheritHandle = True ' the default value for this setting
' Create or open the registry key
retval = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_WRITE, secattr, hregkey, neworused)
If retval <> 0 Then ' error during open
Debug.Print "Error opening or creating registry key -- aborting."
End ' terminate the program
End If
' Write the string to the registry. Note that because Visual Basic is being used, the string passed to the
' function must explicitly be passed ByVal.
stringbuffer = "Rimmer" & vbNullChar ' note how a null character must be appended to the string
retval = RegSetValueEx(hregkey, "username", 0, REG_SZ, stringbuffer, Len(stringbuffer)) ' write the string
' Close the registry key
retval = RegCloseKey(hregkey)
Related Call: RegCreateKeyEx, RegOpenKeyEx
Category: Registry
Back to the index.