Search code examples
c++comatl

How to convert CComBSTR to LPCSTR


I have CComBSTR in my code and have to pass it to function with argument type LPCSTR. How to convert CComBSTR to LPCSTR?


Solution

  • There are many ways to do this, but the ATL way would be using Using MFC MBCS/Unicode Conversion Macros:

    void SomeCode()
    {
        USES_CONVERSION;
        CComBSTR bstr(L"hello world");
        LPCSTR lp = W2CA(bstr); // bstr is a LPWSTR
    }