【问题标题】:C++ code for concatanation of two Bstr strings用于连接两个 Bstr 字符串的 C++ 代码
【发布时间】:2017-01-28 22:41:21
【问题描述】:

我正在尝试连接两个 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();
}

【问题讨论】:

    标签: c++ string com concatenation bstr


    【解决方案1】:

    您可以利用_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();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      相关资源
      最近更新 更多