用于连接两个 Bstr 字符串的 C++ 代码

标签 c++ string com concatenation bstr

我正在尝试连接两个 bstr_t,一个是 char '#',另一个是 int(我将其转换为 bstr_t)并将其作为 bstr 返回(例如,'#'+'1234' 为 '#12345 ').但是,串联后的最终结果仅包含“#”。我不知道我在哪里犯了错误。

function(BSTR* opbstrValue)
{
    _bstr_t sepStr = SysAllocString(_T("#"));

    wchar_t temp_str[20]; // we assume that the maximal string length for displaying int can be 10
    itow_s(imgFrameIdentity.m_nFrameId, temp_str, 10);
    BSTR secondStr = SysAllocString(temp_str);
    _bstr_t secondCComStr; 
    secondCComStr.Attach(secondStr);

    _bstr_t wholeStr = sepStr + secondCComStr;

    *opbstrValue = wholeStr.Detach();
}

最佳答案

您可以利用 _bstr_t 大大简化您的功能的构造函数。 One它的重载将 const _variant_t& 作为输入参数并将其解析为字符串。因此,您的函数可能如下所示:

void function(BSTR* opbstrValue)
{
    _bstr_t res(L"#");
    res += _bstr_t(12345); //replace with imgFrameIdentity.m_nFrameId

    *opbstrValue = res.Detach();
}

关于用于连接两个 Bstr 字符串的 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41915658/

相关文章:

c# - 如何检查 C# 中的字谜字符串

ascii 编码字符串中的 Python 十六进制值

java - 框架/库/api的许可模型

c++ - _IDTExtensibility2,要将什么 DLL 导入到 ATL 项目中?

c++ - 不知道如何正确使用 IShellWindows::Item

c++ - 逐像素复制两个图像

c++ - C++ 中的交互式管道

c++ - 将参数传递给 C++ CORBA 方法实现

c++ - 将类型参数传递给自引用指针

c++ - 从包含十进制数的字符串变量中获取值,该十进制数表示大约 64 GB 的值