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 Long
RegQueryValueEx reads a value from a registry key. It can read many different types of data, including integers, strings, and any other registry data types. When calling the function, the program does not have to know what the data type of the value being read is. Instead, the program receives information telling it what type of data was read.
If an error occued, the function returns a non-zero error code. If successful, the function returns 0.
When putting the data read from the registry into a string, the lpData parameter must be prefixed by the ByVal keyword. The ByVal keyword is not necessary with any other data types passed as that parameter.
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_DYN_DATA = &H80000006
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_USERS = &H80000003
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_DWORD_BIG_ENDIAN = 5
Const REG_DWORD_LITTLE_ENDIAN = 4
Const REG_EXPAND_SZ = 2
Const REG_LINK = 6
Const REG_MULTI_SZ = 7
Const REG_NONE = 0
Const REG_RESOURCE_LIST = 8
Const REG_SZ = 1
Open the registry key HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config and read the value named "username" stored in it. The example given for the RegSetValueEx function creates this key and sets the value appropriately. The example runs when button Command1 is pressed. To run this example, you need to place a command button named Command1 inside a form window.
' 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 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
Public 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 Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Const HKEY_CURRENT_USER = &H80000001
Public Const KEY_READ = &H20019
Public Const REG_SZ = 1
' *** Place the following code inside the form window. ***
Private Sub Command1_Click()
Dim hKey As Long ' receives a handle to the newly created or opened registry key
Dim subkey As String ' name of the subkey to open
Dim stringbuffer As String ' receives data read from the registry
Dim datatype As Long ' receives data type of read value
Dim slength As Long ' receives length of returned data
Dim retval As Long ' return value
' Set the name of the new key and the default security settings
subkey = "Software\MyCorp\MyProgram\Config"
' Create or open the registry key
retval = RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, KEY_READ, hKey)
If retval <> 0 Then
Debug.Print "ERROR: Unable to open registry key!"
Exit Sub
End If
' Make room in the buffer to receive the incoming data.
stringbuffer = Space(255)
slength = 255
' Read the "username" value from the registry key.
retval = RegQueryValueEx(hKey, "username", 0, datatype, ByVal stringbuffer, slength)
' Only attempt to display the data if it is in fact a string.
If datatype = REG_SZ Then
' Remove empty space from the buffer and display the result.
stringbuffer = Left(stringbuffer, slength - 1)
Debug.Print "Username: "; stringbuffer
Else
' Don't bother trying to read any other data types.
Debug.Print "Data not in string format. Unable to interpret data."
End If
' Close the registry key.
retval = RegCloseKey(hKey)
End Sub
Go back to the Function listing.
Go back to the Reference section index.
Last Modified: January 21, 2001
This page is copyright © 2001 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
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/regqueryvalueex.html