【问题标题】:Convert LPWSTR to string将 LPWSTR 转换为字符串
【发布时间】:2012-09-09 18:07:19
【问题描述】:

函数CommandLineToArgvW 给我LPWSTR 类型的命令行参数。我需要string 中的这些参数。 有人能告诉我如何将LPWSTR 转换为string吗?
我正在使用mingw。

【问题讨论】:

  • std::wstring someParam = std::wstring(argv[0]);

标签: c++ winapi mingw


【解决方案1】:
std::string MyString = CW2A (L"LPWSTR STRING");

您需要为CW2A 添加atlstr.h

【讨论】:

  • CW2A 宏将宽字符串转换为 ASCII 字符串,那么为什么要将结果粘贴回 wstring
  • CW2A 的#include 是什么?
  • @ErikAronesty atlstr.h
  • 这不适合日文字符。
【解决方案2】:

尝试使用以下 API 函数:

  1. WideCharToMultiByte

  2. wcstombs

以及两种方法的比较WideCharToMultiByte() vs. wcstombs()

【讨论】:

    【解决方案3】:

    假设你的 LPWSTR 变量是 myVarL:

    wstring ws( myVarL ); 
    string myVarS = string( ws.begin(), ws.end() );
    

    应该做你想要的

    【讨论】:

    • 这将无法正确处理宽字符串中的大多数可能字符
    猜你喜欢
    • 2014-07-31
    • 2010-11-14
    • 1970-01-01
    • 2016-06-07
    • 2011-10-15
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多