从 LPCTSTR 转换为 wchar* [英] Convert from a LPCTSTR to a wchar*

查看:36
本文介绍了从 LPCTSTR 转换为 wchar*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 应用程序,我需要将 LPCTSTR 转换为 wchar*.

I have a c++ application, and i need to convert a LPCTSTR to a wchar*.

是否有执行此转换的函数?

Is there function to perform this conversion?

使用 Visual Studio 2k8.

Using Visual Studio 2k8.

谢谢

推荐答案

根据评论,您正在为 Unicode 进行编译.在这种情况下,LPCTSTR 计算为 const wchar_t*,因此不需要转换.如果您需要一个可修改的缓冲区,那么您可以分配一个并执行内存复制.这是有效的,因为字符串已经用 UTF-16 编码.

From the comments, you are compiling for Unicode. In which case LPCTSTR evaluates as const wchar_t* and so no conversion is necessary. If you need a modifiable buffer, then you can allocate one and perform a memory copy. That works because the string is already encoded in UTF-16.

由于您使用的是 C++,因此将字符串存储在字符串类中而不是使用原始 C 字符串是有意义的.例如,您可以使用 std::wstring.或者您可以使用 MFC/ATL 字符串类.这些选项中的哪一个最适合您取决于您​​的其余代码库的具体情况.

Since you are using C++ it makes sense to store strings in string classes rather than using raw C strings. For example you could use std::wstring. Or your could use the MFC/ATL string classes. Exactly which of these options is best for you depends on the specifics of the rest of your code base.

这篇关于从 LPCTSTR 转换为 wchar*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆