在Win32 DLL中使用WCHAR [英] Using WCHAR in a Win32 Dll
问题描述
我有一个Win32库,字符集预设为 - 使用Unicode字符集,我想使用WCHAR数组来存储字符串文字常量。
例如:
WCHAR string [] =MyWideCharString;
当我编译代码时,我收到错误,指出:
错误 1 错误 C2440:' 初始化':无法从'const char [17]'转换为'WCHAR []'14
我也尝试使用_T宏,但我又遇到了编译错误:
错误 1 错误 C3861:' _ T':标识符不发现14
那么如何我可以在库中使用WCHAR字符数组吗?
问题是字符串文字的语法。类型WCHAR
是字符类型wchar_t
;此类型的字符串文字应以L为前缀:
WCHAR string [] = L MyWideCharString;
您还可以使用便携式声明样式:
_ TCHAR string [] = _T( MyWideCharString);
这里,_TCHAR
和_T
只是宏,它依赖于_UNICODE
的定义。他们进行类型声明char
或w_char
和字符串文字L-prefixed或not,允许你切换从非Unicode(ANSI)到Unicode版本的相同代码。这样,使用宏的类型声明和字符串文字在任何一种情况下都保持一致。
参见 http://msdn.microsoft.com/en-us/library/dybsewaf(v = VS.100)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/dybsewaf(v=VS.100).aspx\"target =_ blanktitle =New Window> ^ ]。
基于_TCHAR
的以空值终止的可移植字符串类型为LPTSTR
和LPCTSTR
,请参阅
http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr [ ^ ]。
-SA
WCHAR string [] = TEXT(MyWideCharString);
Hi,
I have a Win32 library for which the character set is preset to - Use Unicode Character Set and I want to use a WCHAR array for storing a string literal constant.
For example:
WCHAR string[] = "MyWideCharString";
When I compile the code I get the error that states:
Error 1 error C2440: 'initializing' : cannot convert from 'const char [17]' to 'WCHAR []' 14
I tried using _T macro also,but I got a compile error again:
Error 1 error C3861: '_T': identifier not found 14
So how can I use the WCHAR character arrays in my library?
The problem is the syntax for a string literal. The typeWCHAR
is the character typewchar_t
; the string literal for this type should be prefixed with "L":
WCHAR string[] = L"MyWideCharString";
[EDIT]
You can also use portable declaration style:
_TCHAR string[] = _T("MyWideCharString");
Here,_TCHAR
and_T
are just macro which works depending the definition of_UNICODE
. They make type declaration eitherchar
orw_char
and the string literal either L-prefixed or not, which allows you to switch from non-Unicode (ANSI) to Unicode version of the same code. This way, the type declarations and string literal using the macro remain consistent in either case.
See http://msdn.microsoft.com/en-us/library/dybsewaf(v=VS.100).aspx[^].
Null-terminated portable string types based on_TCHAR
areLPTSTR
andLPCTSTR
, see
http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr[^].
—SA
WCHAR string[] = TEXT("MyWideCharString");
这篇关于在Win32 DLL中使用WCHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!