vc++ - 如何将 CString 转换为 LPCWSTR

2022-01-12 00:00:00 visual-c++ c++ mfc

我试图这样做,但我没有找到任何方法.我问这个是因为我是 Windows 新手.我尝试了 stl-strings,但 Visual Studio 2008- 在 stl-wstring-handling 中积累了错误.稍后我会在其他问题上说很多关于这件事的内容.现在有人可以阐明这个问题吗?

I tried to do this , but I didn't find any method for this. I am asking this due to the fact that I am new to windows. I tried stl-strings, but visual studio 2008- accumulates bugs in stl-wstring-handling. I will say a lot about that thing later, in other question. Now Can anybody shed light on this issue?

推荐答案

最简单的方法是使用 MFC 字符串转换宏,定义在:

The easiest way is to use the MFC String Conversion Macros, defined at:

https:///docs.microsoft.com/en-us/cpp/atl/reference/string-conversion-macros?view=msvc-160

例如,将CString转换为LPCWSTR的宏是CT2W(s).

For example, the macro to convert CString to LPCWSTR is CT2W(s).

另一种方法是使用专门的 CStringACStringW 类.这些是 CString 的相应 ascii 和宽版本,具体取决于您是否使用 UNICODE 标志进行编译.所以你可以使用:

Another way is to use the specialized CStringA and CStringW classes. These are the corresponding ascii and wide versions of CString depending on if you're compile with the UNICODE flag. So you can use:

CString your_string = "blah"
CStringW wide_string = your_string;

获取宽版本的字符串.

相关文章