在Win32 DLL中使用WCHAR [英] Using WCHAR in a Win32 Dll

查看:73
本文介绍了在Win32 DLL中使用WCHAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个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 type WCHAR is the character type wchar_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 either char or w_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 are LPTSTR and LPCTSTR, see
http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr[^].

—SA


WCHAR string[] = TEXT("MyWideCharString");


这篇关于在Win32 DLL中使用WCHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆