I have some methods that return a BSTR
, and I need to concatenate those BSTR
s, but it should have a space between each BSTR
:
BSTR a + " " + BSTR b + " " + BSTR c
+ and so on...
I found a function that concatenates two BSTR
s, however I cannot insert a space between each BSTR
.
Sample code:
static BSTR Concatenate2BSTRs(BSTR a, BSTR b)
{
auto lengthA = SysStringLen(a);
auto lengthB = SysStringLen(b);
auto result = SysAllocStringLen(NULL, lengthA + lengthB);
memcpy(result, a, lengthA * sizeof(OLECHAR));
memcpy(result + lengthA, b, lengthB * sizeof(OLECHAR));
result[lengthA + lengthB] = 0;
return result;
}
Also, I tried to wrap the first BSTR
into a _bstr_t
then use the +=
operator to insert a space, but the first BSTR
value is lost.
Any help?
Update
@Remy Lebeau answer works.
However, I tried to do the same steps, but for 6 BSTRs, and it outputs empty string!
Code:
memcpy(result, a, lengthA * sizeof(OLECHAR));
result[lengthA] = L' ';
memcpy(result + lengthA+1, b, lengthB * sizeof(OLECHAR));
result[lengthB] = L' ';
memcpy(result + lengthB+1, c, lengthC * sizeof(OLECHAR));
result[lengthC] = L' ';
memcpy(result + lengthC+1, d, lengthD * sizeof(OLECHAR));
result[lengthD] = L' ';
memcpy(result + lengthD+1, e, lengthE * sizeof(OLECHAR));
result[lengthE] = L' ';
memcpy(result + lengthE+1, f, lengthF * sizeof(OLECHAR));
result[lengthE + 1 + lengthF] = L'\0';
Thank you very much.