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

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

    BSTR to std::wstring:

    // given BSTR bs
    assert(bs != nullptr);
    std::wstring ws(bs, SysStringLen(bs));
    

     
    std::wstring to BSTR:

    // given std::wstring ws
    assert(!ws.empty());
    BSTR bs = SysAllocStringLen(ws.data(), ws.size());
    

    Doc refs:

    1. std::basic_string::basic_string(const CharT*, size_type)
    2. std::basic_string<>::empty() const
    3. std::basic_string<>::data() const
    4. std::basic_string<>::size() const
    5. SysStringLen()
    6. SysAllocStringLen()

提交回复
热议问题