【问题标题】:Convert WCHAR[260] to std::string将 WCHAR[260] 转换为 std::string
【发布时间】:2012-04-29 13:19:07
【问题描述】:

我在 Windows 上从 (PROCESSENTRY32) pe32.szExeFile 获得了 WCHAR[MAX_PATH]。以下方法不起作用:

std::string s;
s = pe32.szExeFile; // compile error. cast (const char*) doesnt work either

std::string s;
char DefChar = ' ';
WideCharToMultiByte(CP_ACP,0,pe32.szExeFile,-1, ch,260,&DefChar, NULL);
s = pe32.szExeFile;

【问题讨论】:

  • 你真的需要std::string吗?它应该直接转换为std::wstring,如std::wstring s(pe32.szExeFile);

标签: c++ windows string wchar


【解决方案1】:

对于您的第一个示例,您可以这样做:

std::wstring s(pe32.szExeFile);

第二个:

char DefChar = ' ';
WideCharToMultiByte(CP_ACP,0,pe32.szExeFile,-1, ch,260,&DefChar, NULL);
std::wstring s(pe32.szExeFile);

因为std::wstring 有一个char* ctor

【讨论】:

  • 两者都没有同样的错误。错误:C2664:'std::basic_string<_elem>::basic_string(std::basic_string<_elem>::_Has_debug_it)':无法将参数 1 从 'WCHAR [260]' 转换为' std::basic_string<_elem>
  • 对不起应该使用 wstring 版本,现在应该编译
  • @user1334943 可以和std::wstring s(&amp;pe32.szExeFile)一起使用吗?
【解决方案2】:

您对WideCharToMultiByte 的调用看起来正确,前提是ch 是 足够大的缓冲区。但是,在此之后,您想要分配 缓冲区(ch)到字符串(或使用它来构造字符串),而不是 pe32.szExeFile

【讨论】:

    【解决方案3】:

    有方便的conversion classes from ATL;您可能想要使用其中的一些,例如:

    std::string s( CW2A(pe32.szExeFile) );
    

    但请注意,从 Unicode UTF-16 到 ANSI 的转换可能有损。如果您不想进行无损转换,您可以从 UTF-16 转换为 UTF-8,并将 UTF-8 存储在 std::string 中。

    如果您不想使用 ATL,有一些方便的免费 C++ 包装器围绕原始 Win32 WideCharToMultiByteconvert from UTF-16 to UTF-8 using STL strings

    【讨论】:

      【解决方案4】:
      #ifndef __STRINGCAST_H__
      #define __STRINGCAST_H__
      
      #include <vector>
      #include <string>
      #include <cstring>
      #include <cwchar>
      #include <cassert>
      
      template<typename Td>
      Td string_cast(const wchar_t* pSource, unsigned int codePage = CP_ACP);
      
      #endif // __STRINGCAST_H__
      
      template<>
      std::string string_cast( const wchar_t* pSource, unsigned int codePage )
      {
          assert(pSource != 0);
          size_t sourceLength = std::wcslen(pSource);
          if(sourceLength > 0)
          {
              int length = ::WideCharToMultiByte(codePage, 0, pSource, sourceLength, NULL, 0, NULL, NULL);
              if(length == 0)
                  return std::string();
      
              std::vector<char> buffer( length );
              ::WideCharToMultiByte(codePage, 0, pSource, sourceLength, &buffer[0], length, NULL, NULL);
      
              return std::string(buffer.begin(), buffer.end());
          }
          else
              return std::string();
      
      }
      

      并按照以下方式使用此模板

      PWSTR CurWorkDir;
      std::string CurWorkLogFile;
      
      CurWorkDir = new WCHAR[length];
      
      CurWorkLogFile = string_cast<std::string>(CurWorkDir);
      
      ....
      
      
      delete [] CurWorkDir;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        • 2014-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-28
        相关资源
        最近更新 更多