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

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");
                  GetErrorDetail();
                  goto CleanUp;
            }
      


      hResult = GetLPBYTEtoOctetString(&var, //IN. Pointer to variant containing the octetstring.
                           &pByte, //OUT. Return LPBYTE to the data represented in octetstring.
                           &dwSize
                           );
--
Thanks.
lapuccaAsked:
Who is Participating?
 
jkrCommented:
>>    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);
memcpy(buf,pByte,dwSize);

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

delete [] buf;

to be absolutely certain it is terminated correctly.
0
 
jkrCommented:
'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

0
 
lapuccaAuthor Commented:
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;
0
 
lapuccaAuthor Commented:
Good idea.  Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.