RegCloseKey Function

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Platforms

Description & Usage

RegCloseKey closes a registry key that had previously been opened with RegCreateKeyEx or RegOpenKeyEx. Your program should use this function once it is finished using the registry key, in order to free resources. Obviously, you can no longer use the key after closing it.

Return Value

If successful, the function returns zero. If an error occured, the function returns a non-zero error code.

Visual Basic-Specific Issues

None.

Parameters

hKey
A handle to the registry key to close.

Example

This example creates a new registry called "HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config". Under that key, it creates a "username" value and sets its value to the string "Rimmer". Place a command button named Command1 in a form window to run this example. The example executes when Command1 is clicked.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type SECURITY_ATTRIBUTES
	nLength As Long
	lpSecurityDescriptor As Long
	bInheritHandle As Long
End Type
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal _
	hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass _
	As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes _
	As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Public 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 Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Const HKEY_CURRENT_USER = &H80000001
Public Const KEY_WRITE = &H20006
Public Const REG_SZ = 1

' *** Place the following code inside the form. ***

Private Sub Command1_Click()
	Dim hKey As Long            ' receives handle to the registry key
	Dim secattr As SECURITY_ATTRIBUTES  ' security settings for the key
	Dim subkey As String        ' name of the subkey to create or open
	Dim neworused As Long       ' receives flag for if the key was created or 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)
	secattr.lpSecurityDescriptor = 0
	secattr.bInheritHandle = 1
	
	' Create (or open) the registry key.
	retval = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_WRITE, _
		secattr, hKey, neworused)
	If retval <> 0 Then
		Debug.Print "Error opening or creating registry key -- aborting."
		Exit Sub
	End If
	
	' Write the string to the registry.  Note the use of ByVal in the second-to-last
	' parameter because we are passing a string.
	stringbuffer = "Rimmer" & vbNullChar  ' the terminating null is necessary
	retval = RegSetValueEx(hKey, "username", 0, REG_SZ, ByVal stringbuffer, _
		Len(stringbuffer))
	
	' Close the registry key.
	retval = RegCloseKey(hKey)
End Sub

See Also

RegCreateKeyEx, RegOpenKeyEx

Category

Registry

Go back to the Function listing.
Go back to the Reference section index.


Last Modified: July 30, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/r/regclosekey.html