如何在 C++ (Unicode) 中将 std::string 转换为 LPCWSTR [英] How to convert std::string to LPCWSTR in C++ (Unicode)

查看:32
本文介绍了如何在 C++ (Unicode) 中将 std::string 转换为 LPCWSTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将 std::string 转换为 LPCWSTR 的方法或代码片段

I'm looking for a method, or a code snippet for converting std::string to LPCWSTR

推荐答案

感谢提供指向 MSDN 文章的链接.这正是我要找的.

Thanks for the link to the MSDN article. This is exactly what I was looking for.

std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

std::wstring stemp = s2ws(myString);
LPCWSTR result = stemp.c_str();

这篇关于如何在 C++ (Unicode) 中将 std::string 转换为 LPCWSTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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