動(dòng)機(jī)
:
在沒(méi)有裝中文字體的機(jī)器上顯示中文
如果不是unicode,某些中文字符會(huì)在不同的codepage上出現(xiàn)亂碼;如果當(dāng)前codepage為韓文時(shí),某些中文字會(huì)變成韓文的樣子,奇怪.
Step1: define _UNICODE, undefine _MBCS
宏定義UNICODE, _UNICODE定義兩個(gè)也可以,定義一個(gè)也可以,windows的頭文件會(huì)保證兩個(gè)都會(huì)被定義.
#ifdef?_UNICODE
#ifndef?UNICODE
#define
?UNICODE?
//
?UNICODE?is?used?by?Windows?headers
#endif
#endif
?
#ifdef?UNICODE
#ifndef?_UNICODE
#define
?_UNICODE????????
//
?_UNICODE?is?used?by?C-runtime/MFC?headers
#endif
#endif
?
Step2:
更改變量類型
比如
char -> TCHAR
但是
,并不是說(shuō)所有的char類型都不能用了.如果需要用,并且函數(shù)的參數(shù)/返回值什么的都不沖突,char類型還是可以用的.
Step3:
更改常量字符串形式
"
abc
" -> _T("
abc
")
因?yàn)殚_(kāi)始寫程序時(shí)沒(méi)有注意
,造成這樣的常量字符串形式非常多,一個(gè)個(gè)改起來(lái)很煩,我寫了個(gè)小插件,可以講
”
abc
”
選中后點(diǎn)一下
button就能變成_T(
“
abc
”
),有點(diǎn)用,代碼也非常的短.
Step4:
替換某些函數(shù)
比如atoi -> _ttoi
在這里遇到一個(gè)問(wèn)題,就是atof的替換.
按照msdn, atof應(yīng)該被_ttof替換
TCHAR.H routine
|
_UNICODE & _MBCS not defined
|
_MBCS defined
|
_UNICODE defined
|
_tstof
|
atof
|
atof
|
_wtof
|
但是奇怪的是,VC6里面不認(rèn)識(shí)_tstof這個(gè)函數(shù),VC.net 2003就可以.
有兩個(gè)解決的辦法:
1)????? 因?yàn)?/span>atof只是替換為浮點(diǎn)數(shù),所以這里用WideCharToMultiByte轉(zhuǎn)化一下之后再調(diào)用atof也是可行的
2)????? 呵呵,用strtod的替換函數(shù)_tcstod,哈哈,當(dāng)然參數(shù)要變一下,我覺(jué)得這個(gè)方法不錯(cuò),不要被msdn限制死了
Step5:
檢查一下調(diào)用其他
dll
的接口函數(shù)
因?yàn)轫?xiàng)目還用了另外一個(gè)dll,其中有些接口需要char*參數(shù),這個(gè)時(shí)候就必須用WideCharToMultiByte轉(zhuǎn)換一下了.
VC里面的設(shè)置:
1) 等把所有的編譯error改完了之后一編譯,除了個(gè)link錯(cuò)誤,不過(guò)msdn有解釋:
If you are using Unicode and MFC, you will get an unresolved external on _WinMain@16 if you don’t create an entrypoint to wWinMainCRTStartup. Use the /ENTRY option or type this value in the Project Settings dialog box. (To find this option in the development environment, click Settings on the Project menu, then click the Link tab, and click Output in the Category box.)

2)終于編譯成功了,調(diào)試的時(shí)候卻發(fā)現(xiàn)CString的值不能在QuickWatch窗口中顯示了
tools->option->
in the debug tab u can find a check box Labeled "Display unicode strings"
Check it.
問(wèn)題
:
直接用
ANSI
的
dll
中的
CString
在使用dll的時(shí)候,需要直接使用dll中的一個(gè)變量,是CString類型,因?yàn)檫@個(gè)dll還沒(méi)有改為unicode,所以在直接使用其變量時(shí)有點(diǎn)問(wèn)題,使得值不對(duì).(是不是這個(gè)原因還沒(méi)有完全驗(yàn)證,應(yīng)該是的)
到這里
,
宣布移植失敗
!
/*補(bǔ)充:
結(jié)果是失敗,但是基本移植代碼的所有步驟都有了。而且,也指出了如果需要調(diào)用的dll不是unicode,每次調(diào)用/傳遞參數(shù)時(shí),都要轉(zhuǎn)換,很煩。
后來(lái)我把dll的代碼也移植了下,現(xiàn)在運(yùn)行的很好,韓文、中文都可以顯示,不管你當(dāng)前語(yǔ)言是什么。
*/
還有一篇講移植步驟的,更全面,適合更復(fù)雜的情況:
Cheat Sheet:Unicode-enabling Microsoft C/C++ Source Code (http://www.i18nguy.com/unicode/c-unicode.html)
?