windows 投射到 LPCWSTR?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/667616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cast to LPCWSTR?
提问by Tony R
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:
我正在尝试创建一个(非常)简单的 Win32 GUI 程序,但由于某种原因,编译器(我使用的是 VC++ 2008 Express)希望我手动将每个字符串或字符 * 类型转换为 LPCWSTR:
I get this compiler error every time I do this, for example I get this error for the "Hello" and "Note":
每次执行此操作时,我都会收到此编译器错误,例如,对于“Hello”和“Note”,我会收到此错误:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'
错误 C2664:“MessageBoxW”:无法将参数 2 从“const char [22]”转换为“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;
}
回答by Stefan
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:
VS2008 中新项目的默认设置是构建支持 UNICODE 的应用程序。您可以更改该默认值并返回使用 ANSI 或 MBCS 应用程序(属性-> 配置属性-> 常规-> 字符集),或使用 Unicode 字符串,如下所示:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, L"Hello", L"Note", MB_OK);
return 0;
}
Do notcast your strings to LPCWSTR because that will lead to undefined behavior! A char is not the same as a wchar_t!
不要不投你的字符串LPCWSTR,因为这会导致不确定的行为!char 与 wchar_t 不同!
回答by Michael Burr
The problem is that you're building for UNICODE and are passing non-Unicode strings.
问题是您正在为 UNICODE 构建并传递非 Unicode 字符串。
Try:
尝试:
MessageBox(NULL, L"Hello", L"Note", MB_OK);
or set up your build for ANSI/MBCS or look into using TCHARs (which are a pain).
或者为 ANSI/MBCS 设置您的构建,或者考虑使用 TCHARs(这很痛苦)。
回答by JasonTrue
My memories of Win32 C programming are hazy, but as I recall, you need to start by wrapping your string literals in this macro:
我对 Win32 C 编程的记忆很模糊,但我记得,您需要首先将字符串文字包装在这个宏中:
_T("mystring")
_T("我的字符串")
When you build unicode, this will be converted to a unicode string.
当您构建 unicode 时,这将被转换为 unicode 字符串。
If you only build unicode, or you are sure that you are only handling a particular string in unicode, you can use the L"" marker, which is what the _T macro does under the covers.
如果您只构建 unicode,或者您确定您只处理 unicode 中的特定字符串,则可以使用 L"" 标记,这是 _T 宏在幕后所做的。
You may need to include the tchar.h header.
您可能需要包含 tchar.h 标头。
When doing win32 programming, I usually declare strings as TCHAR * szWhatever so that things work on Win9x almost as well as NT/Win2k/XP. (There are other convenience macros in there as well such as the LPTSTR and so on, and MFC contains some easy conversion macros for the cases when you actually need to convert between ansi and unicode to call specific APIs).
在进行 win32 编程时,我通常将字符串声明为 TCHAR * szWhatever,以便在 Win9x 上几乎和 NT/Win2k/XP 一样工作。(其中还有其他方便的宏,例如 LPTSTR 等,并且 MFC 包含一些简单的转换宏,适用于您实际上需要在 ansi 和 unicode 之间进行转换以调用特定 API 的情况)。
回答by Saulius ?emaitaitis
As other posts said, you are building a Unicode application. You can switch to and from Unicode project in project settings (don't forget to set it for both "Debug" and "Release" configurations.
正如其他帖子所说,您正在构建一个 Unicode 应用程序。您可以在项目设置中切换到和从 Unicode 项目(不要忘记为“调试”和“发布”配置设置它。
If you want to use it, you'll have to prepend all your static strings with L:
如果要使用它,则必须在所有静态字符串前加上 L:
L"Some static string"
For char[] strings there's a method mbstowcs_s, which is used roughly like this:
对于 char[] 字符串,有一个方法 mbstowcs_s,其用法大致如下:
std::string str; // the string you want to convert
WCHAR wstr[128];
size_t convertedChars = sizeof(wstr)/sizeof(WCHAR);
mbstowcs_s(&convertedChars, wstr, str.c_str(), _TRUNCATE);
This is how I used it in one of my projects. For exact usage refer to MSDN.
这就是我在我的一个项目中使用它的方式。有关确切用法,请参阅MSDN。