一.ANSI和UNICODE
1.為什么要使用Unicode?
(1) 可以很容易地在不同語言之間進行數據交換。
(2) 使你能夠分配支持所有語言的單個二進制.exe文件或DLL文件。
(3) 提高應用程序的運行效率。
Windows 2000是使用Unicode從頭進行開發(fā)的,如果調用任何一個Windows函數并給它傳遞一個ANSI字符串,那么系統(tǒng)首先要將字符串轉換成 Unicode,然后將Unicode字符串傳遞給操作系統(tǒng)。如果希望函數返回ANSI字符串,系統(tǒng)就會首先將Unicode字符串轉換成ANSI字符 串,然后將結果返回給你的應用程序。進行這些字符串的轉換需要占用系統(tǒng)的時間和內存。通過從頭開始用Unicode來開發(fā)應用程序,就能夠使你的應用程序 更加有效地運行。
Windows 98只支持ANSI,只能為開發(fā)ANSI應用程序。 Windows CE 就是使用Unicode的操作系統(tǒng),完全不支持ANSI版函數。
Microsoft將COM從Win16轉換成Win32時,所有COM接口方法都只能接受Unicode字符串。
2.ANSI字符和Unicode字符
ANSI字符類型為CHAR,指向字符串的指針PSTR(LPSTR),指向一個常數字符串的指針PCSTR(LPCSTR);對應的 Windows定義的Unicode字符類型為WCHAR(typedef WCHAR wchar_t),指向Unicode字符串的指針PWSTR ,指向一個常數Unicode字符串的指針PCWSTR 。
ANSI “ANSI”
Unicode L“UNICODE”
ANSI/Unicode T(“string”)或_TEXT(“string”)
3.ANSI字符和Unicode字符串的操作
雙字節(jié)(DBCS)字符集中,字符串的每個字符可以包含一個或兩個字節(jié)。如果只是調用strlen()函數,那么你就無法知道字符串到底有多少個字 符,它只能告訴你到達結尾的0之前有多少個字節(jié)。
標準c中的strcpy,strchr,strcat等只能用于ANSI字符串,不能正確處理Unicode字符串,因此也提供了一組補充函數,功 能等價,但用于Unicode碼。我們來看看string .h字符串頭文件中是怎樣處理char*和wchar_t*兩個字符串版本的:
// …\Microsoft Visual Studio 8\VC\include\string.h
char *strcat(char*,const char*);
wchar_t *wcschr(wchat_t*, const wchar_t*);
類似的還有strchr/wcschr,strcmp/wcscmp,strlen/wcslen etc. ANSI 操作函數以str開頭 strcpy ,Unicode 操作函數以wcs開頭 wcscpy
MBCS 操作函數以_mbs開頭 _mbscpy
ANSI/Unicode 操作函數以_tcs開頭 _tcscpy(C運行期庫)
ANSI/Unicode 操作函數以lstr開頭 lstrcpy(Windows API)
所有新的和未過時的函數在Windows2000中都同時擁有ANSI和Unicode兩個版本。ANSI版本函數結尾以A表示;Unicode版 本函數結尾以W表示。
二.ANSI/UNICODE通用字符/字符串類型TCHAR/LPTSTR/LPCTSTR
Neutral ANSI/UNICODE types
1.通用字符型TCHAR
ifdef UNICODE it is wchar_t(WCHAR)for Unicode platforms;
else it is char for ANSI and DBCS platforms.
2.通用字符串指針LPTSTR
ifdef UNICODE it is LPWSTR(*wchar_t) for Unicode platforms;
else it is LPSTR (*char) for ANSI and DBCS platforms.
3.通用通用常數字符串指針LPCTSTR
ifdef UNICODE it is LPCWSTR(*const wchar_t) for Unicode platforms;
else it is LPCSTR (*const char) for ANSI and DBCS platforms.
typedef LPWSTR LP;
#define __TEXT(quote) L##quote // r_winnt
<1>_UNICODE宏用于C運行期頭文件,UNICODE宏則用于Windows頭文件,當編譯代碼模塊時,通常必須同時定義這兩 個宏。
<2>如果定義了_UNICODE,若要生成一個Unicode字符串,字符串前要加L宏,用于告訴編譯器該字符串應該作為 Unicode字符串來編譯處理。但是這樣又有個問題就是如果沒有定義_UNICODE則編譯出錯。為了解決這個問題我們必須用到_TEXT宏,這個宏也 在TChar.h中做了定義。使用該宏后,無論源文件有沒有定義_UNICODE都不會出現編譯錯誤。
<3>Unicode與ANSI字符串的轉換:Windows函數MultiByteToWideChar/mbstowcs函數用于 將多字節(jié)字符串轉換成寬字符串,函數WideCharToMultiByte/wcstombs將寬字符串轉換成等價的多字節(jié)字符串。
三.ANSI/UNICODE字符串通用函數lstrcmp/lstrcpy/lstrcat/lstrlen
// …\Microsoft Visual Studio 8\VC\PlatformSDK\Include\Winbase.h -- 已經包含在windows.h中。
lstrcmp(lstrcmpi)
WINBASEAPI
int
WINAPI
lstrcmpA(
__in LPCSTR lpString1,
__in LPCSTR lpString2
);
WINBASEAPI
int
WINAPI
lstrcmpW(
__in LPCWSTR lpString1,
__in LPCWSTR lpString2
);
#ifdef UNICODE
#define lstrcmp lstrcmpW
#else
#define lstrcmp lstrcmpA
#endif // !UNICODE
lstrcpy
WINBASEAPI
__out
LPSTR
WINAPI
lstrcpyA(
__out LPSTR lpString1,
__in LPCSTR lpString2
);
WINBASEAPI
__out
LPWSTR
WINAPI
lstrcpyW(
__out LPWSTR lpString1,
__in LPCWSTR lpString2
);
#ifdef UNICODE
#define lstrcpy lstrcpyW
#else
#define lstrcpy lstrcpyA
#endif // !UNICODE
另外還有l(wèi)strcat(W/A)和lstrlen(W/A),這里未列出其函數定義。
四.使用shlwapi頭文件中定義的函數StrCat/StrCmp/StrCpy
shlwapi.dll是UNC和URL地址動態(tài)鏈接庫文件,用于注冊鍵值和色彩設置。因為操作系統(tǒng)字符串函數常常被大型應用程序比如操作系統(tǒng)的外 殼進程Explorer.exe所使用。由于這些函數使用得很多,因此,在應用程序運行時,它們可能已經被裝入RAM。這將有助于稍稍提高應用程序的運行 性能。
// …\Microsoft Visual Studio 8\VC\PlatformSDK\Include\shlwapi.h
注意:使用StrCat、StrCmp、StrCpy etc時要#include "shlwapi.h"
LWSTDAPI_(LPWSTR) StrCatW(LPWSTR psz1, LPCWSTR psz2);
LWSTDAPI_(int) StrCmpW(LPCWSTR psz1, LPCWSTR psz2);
LWSTDAPI_(LPWSTR) StrCpyW(LPWSTR psz1, LPCWSTR psz2);
#ifdef UNICODE
#define StrCat StrCatW
#define StrCmp StrCmpW
#define StrCpy StrCpyW
#else
#define StrCat lstrcatA
#define StrCmp lstrcmpA
#define StrCpy lstrcpyA
五.MFC動態(tài)字符串類CString
// …\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h
一個CString對象由可變長度的一隊字符組成。CString使用類似于Basic的語法提供函數和操作符。連接和比較操作符以及簡化的內存管 理使CString對象比普通字符串數組容易使用。
CString是基于TCHAR數據類型的對象。如果在你的程序中定義了符號_UNICODE,則TCHAR被定義為類型wchar_t,即16位 字符類型;否則,TCHAR被定義為char,即8位字符類型。在UNICODE方式下,CString對象由16位字符組成。非UNICODE方式 下,CString對象由8位字符組成。 而VS2005默認TCHAR是wchar而不是char.
當不使用_UNICODE時,CString是多字節(jié)字符集(MBCS,也被認為是雙字節(jié)字符集,DBCS)。注意,對于MBCS字符 串,CString仍然基于8位字符來計算,返回,以及處理字符串,并且你的應用程序必須自己解釋MBCS的開始和結束字節(jié)。
CString 提供 operator LPCTSTR 來在 CString 和 LPCTSTR 之間進行轉換。
有關CString的操作請參考MSDN MFC類庫。
六.更安全的C語言字符串處理函數 Strsafe.h
// …\Microsoft Visual Studio 8\VC\PlatformSDK\Include\strsafe.h
注意:使用StringCchCopy /StringCchPrintf時要#include "strsafe.h".
STRSAFEAPI是為了解決現有的 C 語言運行時函數的代碼太容易產生的“內存溢出”問題。當我們引用 strsafe 系列函數時,原有的 C 語言字符串處理函數都將被自動進行 #undef 處理。調試過程中的警告或出錯信息將會告訴我們哪些函數哪些不安全,哪些已經被相應的 strsafe 系列函數取代了。
//1.不贊成使用不安全的函數,以避免產生編譯錯誤
//2.如果你不要安全處理,你可以在包含strsafe.h頭文件之前,
#define STRSAFE_NO_DEPRECATE
#ifdef DEPRECATE_SUPPORTED
// First all the names that are a/w variants (or shouldn't be #defined by now anyway).
#pragma deprecated(strcpy)
#pragma deprecated(wcscpy)
#pragma deprecated(lstrcpy)
#pragma deprecated(StrCpy)
類似的Strcat/wcscat/lstrcat/StrCat,sprintf/wsprintf
以下是D3D中預編譯頭文件dxstdafx.h
#pragma warning( disable : 4996 ) //將報警置為無效
#include <strsafe.h>
#pragma warning( default : 4996 ) //將報警置為默認
有關#pragma warning請參考:http://hi.baidu.com/iceland9/blog/item/5af9c0bfd334de0a18d81f33.html
以下是D3D從VS2003移植到VS2005時遇到的安全警告:
warning C4996: 'wcscpy' was declared deprecated
see declaration of 'wcscpy'
Message: 'This function or variable may be unsafe.
Consider using wcscpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
warning C4995: 'lstrcpy': name was marked as #pragma deprecated
warning C4995: 'wsprintf': name was marked as #pragma deprecated
推薦使用新的安全可靠的TRSAFEAPI:
STRSAFEAPI
StringCchCopyA(
__out_ecount(cchDest) STRSAFE_LPSTR pszDest,
__in size_t cchDest,
__in STRSAFE_LPCSTR pszSrc);
STRSAFEAPI
StringCchCopyW(
__out_ecount(cchDest) STRSAFE_LPWSTR pszDest,
__in size_t cchDest,
__in STRSAFE_LPCWSTR pszSrc);
#ifdef UNICODE
#define StringCchCopy StringCchCopyW (W為Wide Unicode)
#else
#define StringCchCopy StringCchCopyA (A為ANSI)
#endif // !UNICODE
#undef strcpy
#define strcpy strcpy_instead_use_StringCbCopyA_or_StringCchCopyA;
#undef wcscpy
#define wcscpy wcscpy_instead_use_StringCbCopyW_or_StringCchCopyW;
#undef wsprintf
#define wsprintf wsprintf_instead_use_StringCbPrintf_or_StringCchPrintf;
// Then all the windows.h names - we need to undef and redef based on UNICODE setting
#undef lstrcpy //取消已定義的宏
#pragma deprecated(lstrcpy) //安全警告
#ifdef UNICODE //使用UNICODE編程
#define lstrcpy lstrcpyW //重定義
#else
#define lstrcpy lstrcpyA //重定義
#endif
類似的有對lstrcat/wsprintf/wvsprintf的#undef,#pragma deprecated,#define。
推薦使用新的安全可靠的TRSAFEAPI:
#undef lstrcpy
#define lstrcpy lstrcpy_instead_use_StringCbCopy_or_StringCchCopy;
// Then the shlwapi names - they key off UNICODE also.
#undef StrCpy
#pragma deprecated(StrCpy)
#ifdef UNICODE
#define StrCpy StrCpyW
#else
#define StrCpy lstrcpyA
#endif
類似的有#undef StrCpyA /StrCpy /StrCatA /StrCat /StrNCat /StrCatN
以及對StrCpy/StrCat/StrNCat的#undef,#pragma deprecated,#define。
推薦使用新的安全可靠的TRSAFEAPI:
#undef StrCpy
#define StrCpy StrCpy_instead_use_StringCbCopy_or_StringCchCopy;
// Then all the CRT names - we need to undef/redef based on _UNICODE value.
參考:
《VC中的__T宏》
http://www.diybl.com/course/3_program/vc/vc_js/2008830/138819.html
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/phunxm/archive/2009/12/26/5082618.aspx