繁体   English   中英

用于合并两个Bstr字符串的C ++代码

[英]C++ code for concatanation of two Bstr strings

我试图连接两个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的构造函数来大大简化您的函数。 一个其重载需要const _variant_t&作为输入参数并且将其解析为一个字符串。 因此,您的函数可能如下所示:

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

    *opbstrValue = res.Detach();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM