在MSDN中,關於函數中字符串長度參數,有這麼一段描述:
When a function has a length parameter for a character string, the length should be documented as a count of TCHAR values in the string. This data type refers to bytes for Windows code page versions of the function or 16-bit
words for Unicode versions. However, functions that require or return pointers to untyped memory blocks, such as the
GlobalAlloc function, generally take a size in bytes, regardless of the prototype that is used. If the allocation of untyped memory is for a string, the application must multiply the
number of characters by sizeof(TCHAR). PS:搜索關鍵詞 爲 Conventions for Function Prototypes
這段描述是說,當一個函數的參數指的是字符串長度時,這個長度指的是該字符串中TCHAR的個數。也就是說,這個數據類型對於Windows code page版本(基本可以理解爲ANSI,但略有不同)是byte的個數,對於Unicode版本是word(16bit)的個數。但是,對於那些需要或返回指向無類型內存塊的指針的函數,例如GlobalAlloc,通常指的就是byte的個數了,而無須去理會原型是什麼。如果無類型內存是爲一個string分配的,那麼,在分配的時候,空間的大小是(等於)string字符數目乘以TCHAR的大小。
WideCharToMultiByte:從Unicode到多字節可能存在部分字符的丟失,因爲多字節的字符譜是Unicode字符譜的子集。
用該函數進行轉換,可以首先用該函數得到待轉換的字節數,然後再用該函數進行轉換。