铸造到LPCWSTR? [英] Cast to LPCWSTR?
问题描述
我想创建一个(非常)简单的Win32 GUI程序,但由于某些原因,编译器(我使用VC ++ 2008防爆preSS)要我手工强制转换每个字符串或字符*到LPCWSTR:
我每次我这样做的时候得到这个编译器错误,比如我得到这个错误,为Hello和注意:
错误C2664:'MessageBoxW':不能从'为const char [22]'到'LPCWSTR
转换参数2请告诉我,我没有投我每次这样做的时候....
这里的code:
的#include<&WINDOWS.H GT;INT WINAPI的WinMain(HINSTANCE的hInstance,HINSTANCE ^ h prevInstance,
LPSTR lpCmdLine,INT的nCmdShow)
{
的MessageBox(NULL,你好,请注意,MB_OK);
返回0;
}
在VS2008新建项目的默认是构建UNI code的应用程序。您可以更改默认设置,回去使用ANSI或MBCS应用程序(属性 - >配置属性 - >常规 - >字符集),或者采用UNI code字符串是这样的:
INT WINAPI的WinMain(HINSTANCE的hInstance,HINSTANCE ^ h prevInstance,
LPSTR lpCmdLine,INT的nCmdShow)
{
的MessageBox(NULL,L你好,L注,MB_OK);
返回0;
}
待办事项的不可以投你的字符串LPCWSTR,因为这会导致不确定的行为!一个char是不一样的wchar_t的!
I'm trying to create a (very) simple Win32 GUI program, but for some reason the compiler (I'm using VC++ 2008 Express) wants me to manually typecast every string or char* to LPCWSTR:
I get this compiler error every time I do this, for example I get this error for the "Hello" and "Note":
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'
Please tell me I don't have to cast every time I do this....
Here's the code:
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Hello", "Note", MB_OK);
return 0;
}
The default for new projects in VS2008 is to build UNICODE aware applications. You can either change that default and go back to using ANSI or MBCS apps (Properties->Configuration Properties->General->Character Set), or use Unicode Strings like this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, L"Hello", L"Note", MB_OK);
return 0;
}
Do not cast your strings to LPCWSTR because that will lead to undefined behavior! A char is not the same as a wchar_t!
这篇关于铸造到LPCWSTR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!