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

后端 未结 4 419
迷失自我
迷失自我 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:21

    You could also do this

    #include 
    
    BSTR bs = SysAllocString("Hello");
    std::wstring myString = _bstr_t(bs, false); // will take over ownership, so no need to free
    

    or std::string if you prefer

    EDIT: if your original string contains multiple embedded \0 this approach will not work.

提交回复
热议问题