動機
:
在沒有裝中文字體的機器上顯示中文
如果不是unicode,某些中文字符會在不同的codepage上出現亂碼;如果當前codepage為韓文時,某些中文字會變成韓文的樣子,奇怪.
Step1: define _UNICODE, undefine _MBCS
宏定義UNICODE, _UNICODE定義兩個也可以,定義一個也可以,windows的頭文件會保證兩個都會被定義.
#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
但是
,并不是說所有的char類型都不能用了.如果需要用,并且函數的參數/返回值什么的都不沖突,char類型還是可以用的.
Step3:
更改常量字符串形式
"
abc
" -> _T("
abc
")
因為開始寫程序時沒有注意
,造成這樣的常量字符串形式非常多,一個個改起來很煩,我寫了個小插件,可以講
”
abc
”
選中后點一下
button就能變成_T(
“
abc
”
),有點用,代碼也非常的短.
Step4:
替換某些函數
比如atoi -> _ttoi
在這里遇到一個問題,就是atof的替換.
按照msdn, atof應該被_ttof替換
TCHAR.H routine
|
_UNICODE & _MBCS not defined
|
_MBCS defined
|
_UNICODE defined
|
_tstof
|
atof
|
atof
|
_wtof
|
但是奇怪的是,VC6里面不認識_tstof這個函數,VC.net 2003就可以.
有兩個解決的辦法:
1)????? 因為atof只是替換為浮點數,所以這里用WideCharToMultiByte轉化一下之后再調用atof也是可行的
2)????? 呵呵,用strtod的替換函數_tcstod,哈哈,當然參數要變一下,我覺得這個方法不錯,不要被msdn限制死了
Step5:
檢查一下調用其他
dll
的接口函數
因為項目還用了另外一個dll,其中有些接口需要char*參數,這個時候就必須用WideCharToMultiByte轉換一下了.
VC里面的設置:
1) 等把所有的編譯error改完了之后一編譯,除了個link錯誤,不過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)終于編譯成功了,調試的時候卻發現CString的值不能在QuickWatch窗口中顯示了
tools->option->
in the debug tab u can find a check box Labeled "Display unicode strings"
Check it.
問題
:
直接用
ANSI
的
dll
中的
CString
在使用dll的時候,需要直接使用dll中的一個變量,是CString類型,因為這個dll還沒有改為unicode,所以在直接使用其變量時有點問題,使得值不對.(是不是這個原因還沒有完全驗證,應該是的)
到這里
,
宣布移植失敗
!
/*補充:
結果是失敗,但是基本移植代碼的所有步驟都有了。而且,也指出了如果需要調用的dll不是unicode,每次調用/傳遞參數時,都要轉換,很煩。
后來我把dll的代碼也移植了下,現在運行的很好,韓文、中文都可以顯示,不管你當前語言是什么。
*/
還有一篇講移植步驟的,更全面,適合更復雜的情況:
Cheat Sheet:Unicode-enabling Microsoft C/C++ Source Code (http://www.i18nguy.com/unicode/c-unicode.html)
?