69,369
社区成员
发帖
与我相关
我的任务
分享
CHAR srcGbk[] =
{
0x64, 0x3a, 0x5c, 0xd0, 0xc2, 0x32, 0x00,
};
HKEY hPrimKey = HKEY_CURRENT_USER;
LPCWSTR szSubKey = L"Software\\myTest";
LPCWSTR szValueName = L"TestValule";
const int CP_GBK = 936;
//写入测试
{
//GBK-->UNICODE
int iWLen = MultiByteToWideChar(CP_GBK, 0, srcGbk, -1, NULL, 0);
WCHAR *wChString = new WCHAR[iWLen + 1];
MultiByteToWideChar(CP_GBK, 0, srcGbk, -1, wChString, iWLen);
wChString[iWLen] = 0;
//UNICODE-->UTF8
int iUtf8Len = WideCharToMultiByte(CP_UTF8, 0, wChString, -1, NULL, 0, NULL, NULL);
CHAR *szUtf8String = new CHAR[iUtf8Len + 1];
WideCharToMultiByte(CP_UTF8, 0, wChString, -1, szUtf8String, iUtf8Len, NULL, NULL);
szUtf8String[iUtf8Len] = 0;
HKEY hKey = NULL;
DWORD dwDisp=0;
if(RegCreateKeyExW(hPrimKey, szSubKey, NULL, NULL,
0, KEY_ALL_ACCESS , NULL, &hKey, &dwDisp) == ERROR_SUCCESS)
{
RegSetValueExW(hKey, szValueName, NULL, REG_SZ, (BYTE*)szUtf8String, iUtf8Len);
RegCloseKey(hKey);
}
delete [] szUtf8String;
delete [] wChString;
}
//读出测试
{
HKEY hKey = NULL;
if(RegOpenKeyExW(hPrimKey, szSubKey, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
//获取长度
DWORD dwType=0, iUtf8Len = 0;
RegQueryValueExW(hKey, szValueName, NULL, &dwType, NULL, &iUtf8Len);
//读入UTF8
CHAR *chUtf8 = new char[iUtf8Len + 1];
RegQueryValueExW(hKey, szValueName, NULL, &dwType, (BYTE*)chUtf8, &iUtf8Len);
chUtf8[iUtf8Len] = 0;
//UTF8-->Unicode
int iWLen = MultiByteToWideChar(CP_UTF8, 0, chUtf8, -1, NULL, 0);
WCHAR *wChString = new WCHAR[iWLen + 1];
MultiByteToWideChar(CP_UTF8, 0, chUtf8, -1, wChString, iWLen);
wChString[iWLen]= 0;
//Unicode--->GBK
int iGBKLen = WideCharToMultiByte(CP_GBK, 0, wChString, -1, NULL, 0, NULL, NULL);
CHAR *chGBK = new CHAR[iGBKLen + 1];
WideCharToMultiByte(CP_GBK, 0, wChString, -1, chGBK, iGBKLen, NULL, NULL);
chGBK[iGBKLen] = 0;
ASSERT(strcmp(srcGbk, chGBK) == 0);
delete [] chGBK;
delete [] wChString;
delete [] chUtf8;
RegCloseKey(hKey);
}
}