Search code examples
c++bstr

How to Convert int to BSTR?


I have a question how can BSTR accepts a number the second line here gives an error

    unsigned int number =10;
    BSTR mybstr=SysAllocString(number);

also this line gives an error

VarBstrCat(mybstr, number, &mybstr);

Thank you :) your help will be greatly appreciated :)


Solution

  • You need to convert the number to a unicode string before you can get a BSTR of it. For this you can use _itow which converts an int into a unicode string.

    unsigned int number = 10;
    wchar_t temp_str[11]; // we assume that the maximal string length can be 10
    _itow(number, temp_str, 10);
    BSTR mybstr = SysAllocString(temp_str);