c++visual-c++visual-c++-6

CComBSTR::Detach and m_str differ


I converteted an old VC++6 lib to VC++17, now I have some issues with BSTR. See the method:

    void GetLastErrorStringInternal(BSTR *LastErrorString)
    {
       CComBSTR cbsErrMsg;

       /*cbsErrMsg gets determined here*/

      std::wcout << "cbsErrMsg = "<< cbsErrMsg.m_str << std::endl;
      *LastErrorString = cbsErrMsg.Detach();
      std::wcout << "LastErrorString = "<< LastErrorString << std::endl;
    }

This puts out:

     cbsErrMsg = No error occured
     LastErrorString = 0278EF88

According to the documentation CComBSTR::Detach "Detaches m_str from the CComBSTR object and sets m_str to NULL." So both outputs should be equal, but they aren't. What am I missing?

Note: Working mainly with C# I'm rather unfamiliar with all the C/C++ string types.

EDIT: Anders and Joseph are absolutely right, I have to dereference LastErrorString! One question remains: In the original implementation this works without dereferencing:

 void GetLastErrorString(BSTR *LastErrorString) //!< [out,retval] error description 
   {
    /*...*/
    CString cstMsg;
    GetLastErrorStringInternal(LastErrorString);
    cstMsg.Format("GetLastErrorString(). Error number: %ld, error message: %S.",m_ptwRSLastError,LastErrorString);
   }

Why does this work?


Solution

  • *LastErrorString as in ...

    std::wcout << "LastErrorString = "<< *LastErrorString << std::endl; // dereference LastErrorString