代码之家  ›  专栏  ›  技术社区  ›  Simsons

如何从CString转换为PCWSTR

  •  1
  • Simsons  · 技术社区  · 14 年前

    我有以下方法:

        VariantFromString(strXMLPath ,vXMLSource); 
    

    方法的签名是:

    HRESULT VariantFromString(PCWSTR wszValue, VARIANT &Variant);
    

    现在,当我通过一个CString时,如下所示:

    char cCurrentPath[FILENAME_MAX];
    
            if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
                {
                    return errno;
                }
    CString strXMLPath = cCurrentPath;
    strXMLPath += XMLFILE;
    VariantFromString(strXMLPath ,vXMLSource);
    

    我收到错误:无法从CString转换为PCWSTR

    1 回复  |  直到 14 年前
        1
  •  3
  •   tenfour    14 年前

    你真的应该使用Unicode( wchar_t 而不是 char ). 这就是操作系统内部操作的方式,并且可以避免在这样的char类型之间不断地转换。

    但在这种情况下,你可以使用 CString::AllocSysString 将其转换为 BSTR ,它与 PCWSTR . 只要确保它被释放 SysFreeString .

    [编辑] 例如,可以将函数更改为:

    VARIANT VariantFromString(const CString& str)
    {
        VARIANT ret;
        ret.vt = VT_BSTR;
        ret.bstrVal = str.AllocSysString();
        return ret;
    }