Unicode时RegSetValueEx注意事项

来源:互联网 发布:php从入门到精通第5版 编辑:程序博客网 时间:2024/06/05 11:13

    RegSetValueEx中最后两个参数Lpdata和cbDate需要特别注意。

  Lpdata:当dwType为REG_SZ或REG_MULTI_SZ时,若定义了unicode则该参数应为wchar_t*型字符串(以NULL结尾);

                   若没有定义unicode则为char *型字符串(以NULL结尾)。

 cbDate:表示Lpdata的字节长度。若为字符型则需要包含NULL和字符串


调用函数的例子如下:
#001  //打注册表。HKEY_CURRENT_USER/"Software"/"Wincpp"/"testreg"
#002  // /"Windows"//"winsize" = "800*600"
#003  //蔡军生 2007/11/04 QQ:9073204深圳
#004  BOOL WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
#005         LPCTSTR lpszValue)
#006  {
#007         //
#008         LONG lResult;
#009         if (lpszEntry == NULL) //删除整键。
#010         {
#011               HKEY hAppKey = GetAppRegistryKey();
#012               if (hAppKey == NULL)
#013               {
#014                    return FALSE;
#015               }   
#016 
#017               lResult = ::RegDeleteKey(hAppKey, lpszSection);
#018               RegCloseKey(hAppKey);
#019         }
#020         else if (lpszValue == NULL)
#021         {
#022               //删除键值。
#023               HKEY hAppKey = GetAppRegistryKey();
#024               if (hAppKey == NULL)
#025               {
#026                    return FALSE;
#027               }   
#028 
#029               HKEY hSecKey = NULL;
#030               DWORD dw;
#031               RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
#032                    REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
#033                    &hSecKey, &dw);
#034               RegCloseKey(hAppKey);
#035 
#036               if (hSecKey == NULL)
#037               {
#038                    return FALSE;
#039               }   
#040 
#041               //
#042              lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);
#043               RegCloseKey(hSecKey);
#044         }
#045         else
#046         {
#047               //设置键值。
#048               HKEY hAppKey = GetAppRegistryKey();
#049               if (hAppKey == NULL)
#050               {
#051                    return FALSE;
#052               }   
#053 
#054               HKEY hSecKey = NULL;
#055               DWORD dw;
#056               //创建子键。
#057               RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
#058                    REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
#059                    &hSecKey, &dw);
#060               RegCloseKey(hAppKey);
#061 
#062               if (hSecKey == NULL)
#063               {
#064                    return FALSE;
#065               }   
#066 
#067               //设置子键中的项值。
#068              lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,
#069                   (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
#070               RegCloseKey(hSecKey);
#071         }
#072         return lResult == ERROR_SUCCESS;
#073 
#074  }


RegSetValueEx Function

Sets the data and type of a specified value under a registry key.

Syntax

LONG WINAPI RegSetValueEx(  __in        HKEY hKey,  __in_opt    LPCTSTR lpValueName,  __reserved  DWORD Reserved,  __in        DWORD dwType,  __in_opt    const BYTE* lpData,  __in        DWORD cbData);

Parameters

hKey

A handle to an open registry key. The key must have been opened with the KEY_SET_VALUE access right. For more information, seeRegistry Key Security and Access Rights.

This handle is returned by the RegCreateKeyEx,RegCreateKeyTransacted,RegOpenKeyEx, or RegOpenKeyTransacted function. It can also be one of the followingpredefined keys:

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_PERFORMANCE_DATA
HKEY_USERS
lpValueName

The name of the value to be set. If a value with this name is not already present in the key, the function adds it to the key.

If lpValueName is NULL or an empty string, "", the function sets the type and data for the key's unnamed or default value.

For more information, see Registry Element Size Limits.

Registry keys do not have default values, but they can have one unnamed value, which can be of any type.

Reserved

This parameter is reserved and must be zero.

dwType

The type of data pointed to by the lpData parameter. For a list of the possible types, seeRegistry Value Types.

lpData

The data to be stored.

For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters.

cbData

The size of the information pointed to by the lpData parameter, in bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ,cbData must include the size of the terminating null character or characters.

Return Value

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 theFormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.

Remarks

Value sizes are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the file names stored in the registry. This helps the registry perform efficiently. Application elements such as icons, bitmaps, and executable files should be stored as files and not be placed in the registry.

If dwType is the REG_SZ, REG_MULTI_SZ, or REG_EXPAND_SZ type and the ANSI version of this function is used (either by explicitly callingRegSetValueExA or by not defining UNICODE before including the Windows.h file), the data pointed to by thelpData parameter must be an ANSI character string. The string is converted to Unicode before it is stored in the registry.

Note that operations that access certain registry keys are redirected. For more information, seeRegistry Virtualization and 32-bit and 64-bit Application Data in the Registry.


原创粉丝点击