C++ 初始化 LPCTSTR /LPCWSTR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17753607/
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
initializing a LPCTSTR /LPCWSTR
提问by N3rdB0mber
I am having crazy difficulty understanding and getting this to work properly. Basically what it comes down to is that I cannot successfully initialize a variable of this type. It needs to have the contents of say 7&2E25DC9D&0&USB003
.
我在理解和让它正常工作时遇到了疯狂的困难。基本上归结为我无法成功初始化这种类型的变量。它需要有 say 的内容7&2E25DC9D&0&USB003
。
Can anybody please explain/show a proper initialization of this type w/ a similar value? I have looked at all the help on this site and changing my project properties from unicode to multibyte is not solving the issue, and the other solutions aren't working for me either.
任何人都可以解释/显示具有类似值的这种类型的正确初始化吗?我查看了此站点上的所有帮助,将我的项目属性从 unicode 更改为 multibyte 并不能解决问题,其他解决方案对我也不起作用。
As my code sits, here is how I am starting the initialization:
在我的代码中,这是我开始初始化的方式:
LPCTSTR portvalue = new TCHAR[100];
回答by Thomas Russell
As you seem to have gathered, LPCTSTR
and TCHAR
are basically defined as following (LPCTSTR
would be read long pointer to constant TCHAR
):
你似乎已经聚集,LPCTSTR
并TCHAR
基本上定义如下(LPCTSTR
将被读长指针常量TCHAR
):
#ifdef _UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif // _UNICODE
typedef const TCHAR* LPCTSTR;
So you can initialize a LPCTSTR
variable as you would a const wchar_t*
or a const char*
variable, e.g. for unicode:
因此,您可以LPCTSTR
像使用 aconst wchar_t*
或 aconst char*
变量一样初始化变量,例如对于 unicode:
LPCTSTR lpszUnicode = L"Test String";
And for ASCII/MultiByte:
对于 ASCII/MultiByte:
LPCTSTR lpszMultibyte = "Test String";
There are however, useful macros when working with the WinAPI: _T("x")
and TEXT("x")
, which both expand to L"x"
(if your project is set for Unicode) or "x"
(if your project properties are set for Multibyte). So for instance, you could use:
但是,在使用 WinAPI:_T("x")
和 时TEXT("x")
,有一些有用的宏,它们都扩展为L"x"
(如果您的项目设置为 Unicode)或"x"
(如果您的项目属性设置为多字节)。例如,您可以使用:
LPCTSTR lpszTest = _T("Test String");
LPCTSTR lpszTest2 = TEXT("Test String 2");
And this will compile under either unicode or multibyte project settings. As for the reason that there are multiple macros expanding to the same thing, check out this blog post.
这将在 unicode 或多字节项目设置下编译。至于有多个宏扩展到同一事物的原因,请查看这篇博文。
You can also do what you are doing by dynamically allocating memory as you have done, so for instance:
您还可以通过动态分配内存来完成您正在做的事情,例如:
LPTSTR lpszDynamic = new TCHAR[100];
// Do something with lpszDynamic
delete[] lpszDynamic;
However, if you are finding yourself dynamically allocating memory often like this, it might be better to use string classes, such as CString
or std::string
/std::wstring
(I often have the following in my MFC/WinAPI projects):
但是,如果您发现自己经常像这样动态分配内存,则最好使用字符串类,例如CString
or std::string
/ std::wstring
(我的 MFC/WinAPI 项目中经常有以下内容):
namespace std {
#ifdef _UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif // _UNICODE
};
回答by Cory Klein
From this answer, you have to prefix the literal with L
从这个答案中,您必须在文字前加上L
LPCWSTR a = L"TestWindow";
As Frederic pointed out, you can initialize a LPCTSTR
in this manner:
正如 Frederic 所指出的,您可以通过LPCTSTR
以下方式初始化 a :
LPCTSTR s = _T("TestWindow");