您的位置:首页 > 编程语言 > Qt开发

qt中unicode的使用 以及微软unicode函数的使用

2008-11-11 20:33 351 查看
在公司做的一个项目,需要读写注册表.在unicode 处理方面费了不少时间,好不容易搞定,结果被我用以前的结点工程覆盖掉,只好返工重来,结果耗了更多的时间,反复的碰壁,不得不让我对这方面有了些领悟:
int GetReg(QString &StrIp,float& atten)
{
HKEY hKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,L"SOFTWARE//spgt",NULL,KEY_ALL_ACCESS,&hKey) != ERROR_SUCCESS)
return FUNC_ERROR;
DWORD KeyIndex = 0;
LPTSTR lpValueName =new TCHAR[250]; // LPTSTR alternative between LPSTR and LPWSTR,which depends on whether "UNICODE" has been defined
DWORD* lpcValueName = new DWORD; //specify the length of ValueName in "TCHAR"
*lpcValueName = 250;
LPDWORD lpdType = new DWORD;
LPBYTE lpData = new BYTE[1024];//used to get the value of the entry specified by "ValueName"
LPDWORD lpcData = new DWORD;// in/out size of lpdata in Bytes(When the lpdata is a unicode array,it also includes the null-Character)
*lpcData = 1024;
int iflag = 0;
while(RegEnumValue(hKey,KeyIndex++,lpValueName,lpcValueName,NULL,lpdType,lpData,lpcData) == ERROR_SUCCESS)
{
if(lstrcmp(lpValueName,L"ip") == 0) //"L" convert the string from ascii to unicode lstrcmp means an alternative function between lstrcmpA and lstrcmpW
{ // it depends on whether you have defined macro "UNICODE" which is predefined by MicroSoft.It is different from "_UNICODE" which is provided by c/c++ run-time libary
StrIp = QString((QChar*)lpData,(*lpcData)/2-1); // Convert a unicode array to a string
iflag++;
}
if(lstrcmp(lpValueName,L"Level") == 0)
{
QString str = QString((QChar*)lpData,(*lpcData)/2-1);
bool iOk = false;
atten = str.toFloat(&iOk);
if(iOk == false)
{
RegCloseKey(hKey);
return FUNC_ERROR;
}
iflag++;
}
*lpcData = 1024;
*lpcValueName = 250;
}
if(iflag != 2)
{
RegCloseKey(hKey);
return FUNC_ERROR;
}
return FUNC_SUCCESS;
}

另;
从Qstring转换成Unicode数组
BYTE* unicode = (BYTE*) QString("test").unicode();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息