How do I convert a LPBYTE to a bstr_t or PWSTR?

Posted on 2006-06-08
Last Modified: 2012-05-05
Hi, I got back a safearray in my Variant variable.  I need to convert that to a string as in bstr_t or PWSTR.  How can I do that?  

//Get the blob
            bstrAttribute = L"meetingBlob";
            V_VT(&var) = VT_UI1 | VT_ARRAY;

            hResult =ptSyDefaultObject->Get(bstrAttribute.copy(), &var);
            if(hResult!= S_OK)//New Sy Group
                  DisplayError( hResult, L"Error obtaining object information. - LoadDefault");
                  goto CleanUp;

      hResult = GetLPBYTEtoOctetString(&var, //IN. Pointer to variant containing the octetstring.
                           &pByte, //OUT. Return LPBYTE to the data represented in octetstring.
Question by:lapucca
    LVL 86

    Expert Comment

    'VariantChangeType()' can do that, e.g.

    VARIANT vDest;

    VariantChangeType(&vDest,&var,0,VT_BSTR); // create a VARIANT of VT_BSTR as a helper

    _bstr_t bstr(vDest. bstrVal); // access the BSTR in the helper VARIANT and assign it to a _bstr_t

    LPWSTR pwst = (wchar_t*) bstr; // access the wide-char pointer


    Author Comment

    Well, that caused an exception error
    Unhandled exception at 0x77a24844 in mmc.exe: 0xC0000005: Access violation reading location 0x00000110.

    However, I did figured out now.  Thanks.

          LPSTR lpcDef = reinterpret_cast<LPSTR>( pByte);
          _bstr_t blob = lpcDef;
    LVL 86

    Accepted Solution

    >>    LPSTR lpcDef = reinterpret_cast<LPSTR>( pByte);

    That might fail if 'pByte' isn't NULL-terminated, which you cannot be sure of. I'd rather use

    char* buf = new char[dwSize + 1];

    memset(buf,0,dwSize + 1);

         LPSTR lpcDef = reinterpret_cast<LPSTR>( buf);
         _bstr_t blob = lpcDef;

    delete [] buf;

    to be absolutely certain it is terminated correctly.

    Author Comment

    Good idea.  Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now