【问题标题】:convert a STRING from LPCWSTR and vice versa c++从 LPCWSTR 转换字符串,反之亦然 c++
【发布时间】:2016-09-12 21:12:41
【问题描述】:

我想将 LPCWSTR 与一个值进行比较,以确保它们相等。而且我不知道如何比较它。我创建了一个 STRING 值并尝试了各种转换,但没有任何效果。本质上是:

request->id // some LPCWSTR value
STRING str = "value" // String value I want to compare
if (request->id != str)
{
 //Something
}

【问题讨论】:

标签: c++ string visual-studio lpcwstr


【解决方案1】:

启用 MFC/ATL 并使用 CString 对象:

if (CString(request->id) != str)

虽然我也不清楚 STRING 是什么类型。我只会对两者都使用 CString:

STRING str = "value" // String value I want to compare
if (CString(request->id) != str)

或者直接使用文字:

if (CString(request->id) != "value")

【讨论】:

    【解决方案2】:

    使用std::wstring 类型而不是std::string 在这种情况下,您可以使用.c_str() 成员函数来处理或使用MultiByteToWideChar() WinAPI 函数将字符串复制到缓冲区。

    【讨论】:

      【解决方案3】:

      我最终使用这个过程让它工作。

      LPCWSTR lpc = L"字符串"; wcscmp(LPCWSTR, LPCWSTR)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-05
        • 1970-01-01
        • 2011-05-27
        • 2012-05-24
        • 2013-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多