RegQueryValue函数

类别:VC语言 点击:0 评论:0 推荐:
RegQueryValueEx(E)
The RegQueryValueEx function retrieves the type and data for a specified value name associated with an open registry key.
函数RegQueryValueEx找回一个打开的注册表键值相关联的给定的变量数据或者变量。
LONG RegQueryValueEx(
  HKEY hKey,            // handle to key  主键句柄
  LPCTSTR lpValueName,  // value name     子键名称
  LPDWORD lpReserved,   // reserved      保留
  LPDWORD lpType,       // type buffer  
  LPBYTE lpData,        // data buffer   存储返回值的缓冲区
  LPDWORD lpcbData      // size of data buffer存储返回值的缓冲区的大小
);
Parameters
hKey
[in] Handle to a currently open key or one of the following predefined keys:
HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
Windows NT/2000/XP: HKEY_PERFORMANCE_DATA
Windows XP: HKEY_PERFORMANCE_TEXT
Windows XP: HKEY_PERFORMANCE_NLSTEXT
Windows 95/98/Me: HKEY_DYN_DATA
lpValueName
[in] Pointer to a null-terminated string containing the name of the value to query.
If lpValueName is NULL or an empty string, "", the function retrieves the type and data for the key's unnamed or default value, if any.
Windows 95/98/Me: Every key has a default value that initially does not contain data. On Windows 95, the default value type is always REG_SZ. On Windows 98, the type of a key's default value is initially REG_SZ, but RegSetValueEx can specify a default value with a different type.
Windows NT/2000/XP: Keys do not automatically have an unnamed or default value. Unnamed values can be of any type.
lpReserved
Reserved; must be NULL.
lpType
[out] Pointer to a variable that receives a code indicating the type of data stored in the specified value. For a list of the possible type codes, see Registry Value Types. The lpType parameter can be NULL if the type code is not required.
lpData
[in, out] Pointer to a buffer that receives the value's data. This parameter can be NULL if the data is not required.
lpcbData
[in/out] Pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the lpData parameter. When the function returns, this variable contains the size of the data copied to lpData.
If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, then lpcbData will also include the size of the terminating null character or characters.
The lpcbData parameter can be NULL only if lpData is NULL.
If the buffer specified by lpData parameter is not large enough to hold the data, the function returns the value ERROR_MORE_DATA, and stores the required buffer size, in bytes, into the variable pointed to by lpcbData. In this case, the contents of the lpValue buffer are undefined.
If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS, and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This lets an application determine the best way to allocate a buffer for the value's data.
Window NT: If hKey specifies HKEY_PERFORMANCE_DATA and the lpData buffer is too small, RegQueryValueEx returns ERROR_MORE_DATA but lpcbData does not return the required buffer size. This is because the size of the performance data can change from one call to the next. In this case, you must increase the buffer size and call RegQueryValueEx again passing the updated buffer size in the lpcbData parameter. Repeat this until the function succeeds. You need to maintain a separate variable to keep track of the buffer size, because the value returned by lpcbData is unpredictable.
Return Values
If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.
Remarks
The key identified by hKey must have been opened with KEY_QUERY_VALUE access. To open the key, use the RegCreateKeyEx or RegOpenKeyEx function.
If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the proper null-terminating characters. Applications should ensure that the string is properly terminated before using it, otherwise, the application may fail by overwriting a buffer.
If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, and the ANSI version of this function is used (either by explicitly calling RegQueryValueExA or by not defining UNICODE before including the Windows.h file), this function converts the stored Unicode string to an ANSI string before copying it to the buffer pointed to by lpData.
Window NT/2000: When calling the RegQueryValueEx function with hKey set to the HKEY_PERFORMANCE_DATA handle and a value string of a specified object, the returned data structure sometimes has unrequested objects. Don't be surprised; this is normal behavior. When calling the RegQueryValueEx function, you should always expect to walk the returned data structure to look for the requested object.
Windows 95/98/Me: No registry subkey or value name may exceed 255 characters.
Windows 95/98/Me: RegQueryValueExW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
Example Code
For an example, see Retrieving Data from the Registry.
Requirements
  Windows NT/2000/XP: Included in Windows NT 3.1 and later.
  Windows 95/98/Me: Included in Windows 95 and later.
  Header: Declared in Winreg.h; include Windows.h.
  Library: Use Advapi32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
RegQueryValueEx(C)
函数RegQueryValueEx找回一个打开的注册表键值相关联的给定的变量数据或者变量。
LONG RegQueryValueEx(
  HKEY hKey,            //  主键句柄
  LPCTSTR lpValueName,  //     子键名称
  LPDWORD lpReserved,   //     保留
  LPDWORD lpType,       // type buffer  
  LPBYTE lpData,        //   存储返回值的缓冲区
  LPDWORD lpcbData      file://存储返回值的缓冲区的大小
);
参数:
Hkey  指向一个已经打开的或者是下列值之一的句柄
HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
Windows NT/2000/XP: HKEY_PERFORMANCE_DATA
Windows XP: HKEY_PERFORMANCE_TEXT
Windows XP: HKEY_PERFORMANCE_NLSTEXT
Windows 95/98/Me: HKEY_DYN_DATA
LpValueName
指向一个以空字符结束的字符串,该字符串包含要查询的数值名称。
如果lpValueName是NULL或者是空字符串,函数将找回这个指定键值的未命名或者缺省的数值。
Windows NT/2000/XP: Keys do not automatically have an unnamed or default value. Unnamed values can be of any type.
Windows 95/98/Me:  任何一个键在开始都有一个不包含数据缺省的数值类型。在Window95中,缺省的数值类型总是REG_SZ,在Windows98中,键的缺省数据类型初始化为REG_SZ,但是函数RegSetValueEx可以指定一个不同的数据类型作为缺省的数据类型。
Windows NT/2000/XP: 键不能自动的有一个位命名的或者缺省的数据类型。为明明数值可以为任何类型的数据类型
LpReserved
保留的参数,必须为NULL
lpType
[输出]指向接收存储特定数据的数据类型的变量,关于特定类型的列表可以查看注册变量类型。如果这种类型不是必须的,参数lpType可以为NULL.
LpData
指向接收数据值的缓冲区,如果这个数据不是必须的参数可以为NULL.
LpcbData
[输入/输出]  指向一个变量,它表明参数lpData所指向的缓冲区用字节计算的大小。当函数返回时,这个变量包含了lpData中内容的大小。
如果数据类型是REG_SZ, REG_MULTI_SZ 或者REG_EXPAND_SZ,参数lpcbData的大小将包含结束空字符或者空字符串。
如果参数lpData为NULL,参数lpcbData也将为空。
如果参数lpData所指向的缓冲区太小而不能够存储返回的所有数据,函数将返回错误代码ERROR_MORE_DATA,参数lpcbData将存储以字节计算,缓冲区所需要的实际的大小,在这种情况下lpValue所指向的缓冲区将不被定义。
如果lpData为NULL,并且lpcbData不为NULL,函数将返回错误码ERROR_SUCCESS,并且保存以字节计算的索要保存的数据的大小。这将是程序决定分配数据缓冲区的最好方法。
Window NT: 如果键值句柄hKey指向HKEY_PERFORMANCE_DATA并且lpData所指向的缓冲区lpData太小,函数RegQueryValueEx将返回错误码ERROR_MORE_DATA,参数lpcbData将不返回所需要的缓冲区的大小。这是因为运行数据的大小可以随着不同的调用而改变。在这种情况下,你必须增大缓冲区并且重新调用函数RegQueryValueEx,校正参数lpcbData大小,不断的重复这样的操作直到函数运行成功。因为lpcbData所返回的数值是不能与小知道的,因此你需要一个独立的变量来指导缓冲区的大小。
返回值
如果函数运行成功,返回纸将是ERROR_SUCCESS。如果函数运行失败,返回值是一个定义在头文件Winerror.h不为零的错误码。你可以通过函数FormatMessage以标志FORMAT_MESSAGE_FROM_SYSTEM得到一个一般的错误描述。
备注
标志参数hKey的键值被打开必须拥有权利KEY_QUERY_VALUE。打开一个键可以查看函数RegCreateKeyEx 和 RegOpenKeyEx。
如果数据类型为REG_SZ, REG_MULTI_SZ 或者 REG_EXPAND_SZ,字符串可能不被以空字符结束的字符串。应用程序需要在应用它的时候确保该字符串是完全的结束。否则,应用程序可能覆盖了缓冲区而运行失败。
如果数据类型为REG_SZ, REG_MULTI_SZ 或者 REG_EXPAND_SZ,并且函数为ANSI版本(或者明确的调用函数RegQueryValueExA或者在定义头文件Windows.h前没有定义UNICODE) ,
在拷贝到lpData所指向的缓冲区之前函数将保存的Unicode字符串转变成ANSI字符串 。
Window NT/2000:  当将hKey设为句柄HKEY_PERFORMANCE_DATA和一个变量字符串指向一个特定的对象时调用函数RegQueryValueEx,返回的结构体有时却是未被请求的对象,其实这是正常的。当调用函数RegQueryValueEx时,你总是期望返回的数据结构是你需要的对象。(you should always expect to walk the returned data structure to look for the requested object. )
Windows 95/98/Me: 没有注册的子键或者数据值可能超过255个字符。
Windows 95/98/Me:微软用函数 RegQueryValueExW支持Unicode。要想使用这个函数,你必须为你的应用程序添加特定的文件支持。具体清查看微软的Unicode on Windows 95/98/Me Systems。
例子代码:
实例请看 Retrieving Data from the Registry.
需求
  Windows NT/2000/XP: 支持在Windows NT 3.1或者更晚版本.
  Windows 95/98/Me: 支持Windows 95及其更晚版本.
  Header: 在文件 Winreg.h中声明;在头文件中Windows.h定义
  Library: 用Advapi32.lib.
  Unicode: 执行查看 Unicode and ANSI versions on Windows NT/2000/XP也可以查看 Microsoft Layer for Unicode的支持。

本文地址:http://com.8s8s.com/it/it2475.htm