【问题标题】:Dynamically creating "LPCWSTR" variable from std::string variable从 std::string 变量动态创建“LPCWSTR”变量
【发布时间】:2014-01-12 07:22:28
【问题描述】:

目前我有代码 (typedef _Null_terminated_ CONST WCHAR *LPCWSTR;):

LPCWSTR wchar_string(L"ABC");

但需要启用这样的功能:

std::string s("ABC");
...
LPCWSTR wchar_string(/* create somehow from variable s */);

【问题讨论】:

    标签: string winapi type-conversion wchar


    【解决方案1】:

    std::string 是一个 8 位字符串。 wchar_string 是一个宽 (UTF-16) 代码点数组。根据定义,这需要进行转码操作,而不仅仅是简单的分配。

    首先,8 位字符串是如何编码的?是 UTF-8、Windows ANSI“当前代码页”、仅限 Latin-1 还是什么?

    其次,调用类似 Windows 的 MultiByteToWideChar 之类的函数来完成繁重的工作,并确保为 wchar_string 指向的结果缓冲区管理内存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2021-10-26
      • 2011-10-27
      • 1970-01-01
      相关资源
      最近更新 更多