sarah daneshvar
sarah daneshvar

Reputation: 75

C++ code for concatanation of two Bstr strings

I am trying to concatenate two bstr_t one is char '#' and the other is an int (which I convert it to bstr_t) and gonna return it as a bstr (e.g '#'+'1234' as '#12345'). however, after concatenation the final result only contains '#'. I don't know where I am making mistake.

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();
}

Upvotes: 0

Views: 466

Answers (1)

Aurora
Aurora

Reputation: 1354

You can greatly simplify your function by taking advantage of _bstr_t's constructor. One of its overloads takes a const _variant_t& as an input parameter and parses it as a string. Hence, your function could look as follows:

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

    *opbstrValue = res.Detach();
}

Upvotes: 1

Related Questions