在公司做的一个项目,需要读写注册表.在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();
qt中unicode的使用 以及微软unicode函数的使用
最新推荐文章于 2023-09-15 19:22:44 发布