2012年3月16日星期五

VC++ 问题 cannot convert parameter 1 from 'const char [21]' to 'LPCWSTR'

 选择 项目属性--> Character Set --> 选择Use Multi-Byte Character Set
 
 
This error message means that you are trying to pass a multi-byte string (const char [12]) to a function which expects a unicode string (LPCTSTR). The LPCTSTR type extends to const TCHAR*, where TCHAR ischarwhen you compile for multi-byte andwchar_tfor unicode. Since the compiler doesn't accept the char array, we can safely assume that the actual type of TCHAR, in this compilation, is wchar_t.
 
Resolution
You will have to do one of two things:
Change your project configuration to use multibyte strings. Press ALT+F7 to open the properties, and navigate to Configuration Properties > General. Switch Character Set to "Use Multi-Byte Character Set".
Indicate that the string literal, in this case "Hello world!" is of a specific encoding. This can be done through either prefixing it withL, such asL"Hello world!", or surrounding it with the generic_T("Hello world!")macro. The latter will expand to theLprefix if you are compiling for unicode (see #1), and nothing (indicating multi-byte) otherwise.
Variations
Another error message, indicating the same problem, would be:
cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'
Where LPCWSTR maps to a wchar_t pointer, regardless of your build configuration. This problem can be resolved primarily by using solution #2, but in some cases also #1. A lot of the Microsoft provided libraries, such as the Platform SDK, have got two variations of each function which takes strings as parameters. In case of a unicode build, the actual functions are postfixed W, such as the MessageBoxW seen above. In case of multi-byte, the function would be MessageBoxA (ASCII). Which of these functions is actually used when you compile your application, depends on the setting described in resolution #1 above.

没有评论:

发表评论