RegQueryValueEx Function

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

Platforms

Description & Usage

RegQueryValueEx reads a value from a registry key. It can read many different types of data, including integers, strings, and all the 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.

Return Value

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

Visual Basic-Specific Issues

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.

Parameters

hKey
A handle to the registry key to read the value from. This could also be one of the following flags identifying one of the predefined registry base keys:
HKEY_CLASSES_ROOT
The HKEY_CLASSES_ROOT base key.
HKEY_CURRENT_CONFIG
The HKEY_CURRENT_CONFIG base key.
HKEY_CURRENT_USER
The HKEY_CURRENT_USER base key.
HKEY_DYN_DATA
Windows 95, 98: The HKEY_DYN_DATA base key.
HKEY_LOCAL_MACHINE
The HKEY_LOCAL_MACHINE base key.
HKEY_PERFORMANCE_DATA
Windows NT, 2000: The HKEY_PERFORMANCE_DATA base key.
HKEY_USERS
The HKEY_USERS base key.
lpValueName
The name of the value to read.
Reserved
Reserved. Set to 0.
lpType
Receives one of the following flags identifying the data type of the data being read:
REG_BINARY
A non-text sequence of bytes.
REG_DWORD
Same as REG_DWORD_LITTLE_ENDIAN.
REG_DWORD_BIG_ENDIAN
A 32-bit integer stored in big-endian format. This is the opposite of the way Intel-based computers normally store numbers -- the byte order is reversed.
REG_DWORD_LITTLE_ENDIAN
A 32-bit integer stored in little-endian format. This is the way Intel-based computers store numbers.
REG_EXPAND_SZ
A null-terminated string which contains unexpanded environment variables.
REG_LINK
A Unicode symbolic link.
REG_MULTI_SZ
A series of strings, each separated by a null character and the entire set terminated by a two null characters.
REG_NONE
No data type.
REG_RESOURCE_LIST
A list of resources in the resource map.
REG_SZ
A string terminated by a null character.
lpData
Variable or whatever object receives the information read from the registry.
lpcbData
Set this to the length in bytes of whatever was passed as lpData to receive the data read from the registry. This parameter also receives the length in bytes of the data actually read from the registry.

Constant Definitions

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

Example

' Create a key called HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config.
' Then read the string value called "username" and display it.
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  ' receives data read from the registry
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"
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_READ, 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

' Read the "username" string from the registry key.  Note how in Visual Basic the string
' must be passed using the ByVal keyword.
stringbuffer = Space(255)  ' make room in the buffer to receive the information
slength = 255  ' this must be set if passing a string to the function
retval = RegQueryValueEx(hregkey, "username", 0, REG_SZ, ByVal stringbuffer, slength)  ' read data
stringbuffer = Left(stringbuffer, slength)  ' extract the returned data from the buffer
If retval = 0 Then Debug.Print "User name: "; stringbuffer  ' only display it if no error occured

' Close the registry key
retval = RegCloseKey(hregkey)

Related Functions

RegDeleteValue, RegSetValueEx

Category

Registry

Back to the index.


Last Modified: September 11, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information

Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/r/regqueryvalueex.html