gpt4 book ai didi

c++ - 如何从 CString 转换为 PCWSTR

转载 作者:可可西里 更新时间:2023-11-01 11:39:13 26 4
gpt4 key购买 nike

我有以下方法:

    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

最佳答案

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

但在这种情况下,您可以使用 CString::AllocSysString 将其转换为与 PCWSTR 兼容的 BSTR。只需确保使用 SysFreeString 释放它即可。

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

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

关于c++ - 如何从 CString 转换为 PCWSTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4312175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com