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

Posted on 2006-06-08
Medium Priority
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
  • 2
  • 2
LVL 86

Expert Comment

ID: 16864061
'VariantChangeType()' can do that, e.g.


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

ID: 16864479
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

jkr earned 1500 total points
ID: 16864528
>>    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

ID: 16864663
Good idea.  Thank you.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 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