四极管:读取注册表键值函数分析

来源:互联网 发布:巫师2加强版优化补丁 编辑:程序博客网 时间:2024/04/29 23:57

函数:RegOpenKeyEx ;RegQueryValueEx

一、RegOpenKeyEx()

函数功能:打开一个指定的注册表键

2、函数原型:

LONG RegOpenKeyEx(  

    HKEY hKey, // 需要打开的主键的名称  

    LPCTSTR lpSubKey, //需要打开的子键的名称  

    DWORD ulOptions, // 保留,设为0  

    REGSAM samDesired, // 安全访问标记,也就是权限  

    PHKEY phkResult // 得到的将要打开键的句柄

   )

3、函数参数:

hKey   [输入] 当前打开或者以下预定义的键。  

HKEY_CLASSES_ROOT  

HKEY_CURRENT_CONFIG  

HKEY_CURRENT_USER  

HKEY_LOCAL_MACHINE  

HKEY_USERS  

Windows NT/2000/XP: HKEY_PERFORMANCE_DATA   

Windows 95/98/Me: HKEY_DYN_DATA   

lpSubKey   

[输入] 指向一个非中断字符串包含将要打开键的名称。如果参数设置为NULL 或者指向一个空字符串,

过程将打开一个新的句柄由hKey参数确定的值。这种情况下,过程不会关闭先前已经打开的句柄。  

ulOptions   

保留,必须设置为 0   

samDesired   

[输入] 对指定键希望得到的访问权限的访问标记。 这个参数可以使下列值的联合。  

值 Meaning   

KEY_CREATE_LINK 许可创建一个符号连接   

KEY_CREATE_SUB_KEY 许可创建子键  

KEY_ENUMERATE_SUB_KEYS 许可列举子键  

KEY_EXECUTE 许可读访问  

KEY_NOTIFY 许可提供更该通知  

KEY_QUERY_VALUE 许可查询子键数据  

KEY_SET_VALUE 许可设置子键数据  

KEY_ALL_ACCESS 联合了 KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS,   

KEY_NOTIFY, KEY_CREATE_SUB_KEY, KEY_CREATE_LINK,   

KEY_SET_VALUE 访问权限并且加上所有的标准访问权限  

除了SYNCHRONIZE   

KEY_READ 联合了 STANDARD_RIGHTS_READ, KEY_QUERY_VALUE,   

KEY_ENUMERATE_SUB_KEYS,KEY_NOTIFY 访问权限  

KEY_WOW64_64KEY Windows XP: 使64位或者32位应用程序打开64位键  

KEY_WOW64_32KEY Windows XP: 使64位或者32位应用程序打开32位键  

KEY_WRITE 联合 STANDARD_RIGHTS_WRITE, KEY_SET_VALUE,   

KEY_CREATE_SUB_KEY访问权限  phkResult   

[输出] 指向一个被打开键返回的句柄的指针。当不再需要句柄,必须调用 RegCloseKey 关闭它。  

返回值:  如果过程执行成功,返回值是 ERROR_SUCCESS。  

如果功能失败,返回一个非零值,错误码在 Winerror.h 定义。可以使用 FormatMessage 函数 和 FORMAT_MESSAGE_FROM_SYSTEM 标记获得一个分类的错误描述。

二、

RegQueryValueEx

2、函数原型:

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  

);

说明   

获取一个项的设置值   

返回值   Long,零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码   

参数表   参数 类型及说明   

HKEY hKey,一个已打开项的句柄,或者指定一个标准项名   

LPCTSTR lpValueName,要获取值的名字   

LPDWORD lpReserved,未用,设为零   

LPDWORD lpType,用于装载取回数据类型的一个变量   

LPBYTE lpData,用于装载指定值的一个缓冲区   

LPDWORD lpcbData,用于装载lpData缓冲区长度的一个变量。一旦返回,它会设为实际装载到缓冲区的字节数   

lpValueName 指向要查询值的名字的字符串(以空字符结束)。 如果lpValueName是NULL或一个空字符串(""),这个函数找回这个键的未命名或默认值的类型和数据。 

Windows 95和Windows 98: 每个键有一个默认值(最初的不包含数据)。在Windows 95,这人默认值类型总是REG_SZ。

在Windows 98,默认键的类型最初是REG_SZ,但可以通过RegSetValueEx指定一个默认值为不同的类型。 

Windows NT: 键不能自动拥有一个未命名或默认的值,未命名的值可以是任何类型。  lpReserved 保留,必须是NULL.

在Platform Builder fo Microsoft Windows CE 5.0 Help中有如下解析:

LONG RegOpenKeyEx( 

HKEY hKey, 

LPCWSTR lpSubKey, 

DWORD ulOptions, 

REGSAM samDesired, 

PHKEY phkResult 

); 

Parameters 

hKey 

[in] Handle to a currently open key or any of the following predefined reserved handle values: 

HKEY_CLASSES_ROOT 

HKEY_CURRENT_USER 

HKEY_LOCAL_MACHINE 

HKEY_USERS 

Windows CE does not support the HKEY_CURRENT_CONFIG, HKEY_PERFORMANCE_DATA, or HKEY_DYN_DATA predefined reserved handle values. 

lpSubKey 

[in] Pointer to a null-terminated string containing the name of the subkey to open. If this parameter is NULL or a pointer to an empty string, the function will open a new handle to the key identified by the hKey parameter. In this case, the function will not close the handles previously opened. 

ulOptions 

[in] Reserved; set to 0. 

samDesired 

[in] Not supported; set to 0. 

phkResult 

[out] Pointer to a variable that receives a handle to the opened key. When you no longer need the returned handle, call the RegCloseKey function to close it. 

Return Values 

ERROR_SUCCESS indicates success. A nonzero error code defined in Winerror.h indicates failure. To get a generic description of the error, call FormatMessage with the FORMAT_MESSAGE_FROM_SYSTEM flag set. The message resource is optional; therefore, if you call FormatMessage it could fail. 

Remarks 

Unlike the RegCreateKeyEx function, the RegOpenKeyEx function does not create the specified key if the key does not exist in the registry.

----------------------------------------

LONG RegQueryValueEx( 

  HKEY hKey, 

  LPCWSTR lpValueName, 

  LPDWORD lpReserved, 

  LPDWORD lpType, 

  LPBYTE lpData, 

  LPDWORD lpcbData 

); 

Parameters

hKey 

[in] Handle to a currently open key or any of the following predefined reserved handle values: 

HKEY_CLASSES_ROOT 

HKEY_CURRENT_USER 

HKEY_LOCAL_MACHINE 

HKEY_USERS 

lpValueName 

[in] Pointer to a string containing the name of the value to query. If this parameter is NULL or an empty string, the function retrieves the type and data for the key's unnamed value. A registry key does not automatically have an unnamed or default value. Unnamed values can be of any type. 

lpReserved 

[in] Reserved; set to NULL. 

lpType 

[out] Pointer to a variable that receives the type of data associated with the specified value. The following table shows the possible values that lpType can return. Value Description 

REG_BINARY Specifies a binary data in any form. 

REG_DWORD Specifies a 32-bit number. 

REG_DWORD_LITTLE_ENDIAN Specifies a 32-bit number in little-endian format. This is equivalent to REG_DWORD. 

In little-endian format, a multi-byte value is stored in memory from the lowest byte (the little end) to the highest byte. For example, the value 0x12345678 is stored as (0x78 0x56 0x34 0x12) in little-endian format.

 

REG_DWORD_BIG_ENDIAN Specifies a 32-bit number in big-endian format. 

In big-endian format, a multi-byte value is stored in memory from the highest byte (the big end) to the lowest byte. For example, the value 0x12345678 is stored as (0x12 0x34 0x56 0x78) in big-endian format.

 

REG_EXPAND_SZ Specifies a null-terminated string that contains unexpanded references to environment variables. For example, %PATH%. 

REG_LINK Specifies a Unicode symbolic link. Used internally; applications should not use this type. 

REG_MULTI_SZ Specifies an array of null-terminated strings, terminated by two null characters. 

REG_NONE No defined value type. 

REG_RESOURCE_LIST Specifies a device-driver resource list. 

REG_SZ Specifies a null-terminated Unicode string. 

The lpType parameter can be NULL if the type is not required. 

lpData 

[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. 

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. 

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. 

Return Values

ERROR_SUCCESS indicates success. A nonzero error code defined in Winerror.h indicates failure. To get a generic description of the error,

 call FormatMessage with the FORMAT_MESSAGE_FROM_SYSTEM flag set. The message resource is optional; therefore,

if you call FormatMessage it could fail.

原创粉丝点击