BSTR to std::string (std::wstring) and vice versa

后端 未结 4 407
迷失自我
迷失自我 2020-12-02 09:01

While working with COM in C++ the strings are usually of BSTR data type. Someone can use BSTR wrapper like CComBSTR or MS\'s CSt

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 09:25

    There is a c++ class called _bstr_t. It has useful methods and a collection of overloaded operators.

    For example, you can easily assign from a const wchar_t * or a const char * just doing _bstr_t bstr = L"My string"; Then you can convert it back doing const wchar_t * s = bstr.operator const wchar_t *();. You can even convert it back to a regular char const char * c = bstr.operator char *(); You can then just use the const wchar_t * or the const char * to initialize a new std::wstring oe std::string.

提交回复
热议问题