BSTR to BYTE problem

Posted on 2004-04-29
Last Modified: 2012-06-21
Hi, I am new to Visual C++, and I am trying to write a simple routine which will convert a BSTR toa BYTE array, perform some operations on the BYTE array, and then return a BSTR to the calling object. I am attempting to make a COM object using the ATL Wizard, and as I understand it, the BSTR data type is what is normally used. I have Visual Studio 6.0 as well as Visual Studio .NET. From what I can tell, the code would probably be the same on either platform, but I could be wrong.

Anyway, here is what I was trying to do, but it doesnt seem to be working. Again, I am new to Visual C++, so my understanding of streams, etc. is all based off of online examples, so the below code may be totally wrong.

STDMETHODIMP Ctest_meth98::testFunction(BSTR inSt1, BSTR* outSt2)
      BYTE buf1[1000];
      CComBSTR *in_bs1 = new CComBSTR();
      CComBSTR *out_bs2 = new CComBSTR();
      IStream *p_inS, *p_outS;
      ULONG i, j;
      CreateStreamOnHGlobal(NULL, TRUE, &p_inS);

/* At this point I assume that the stream would contain the string value passed in to the BSTR in_bs1 */

/* Seek to beginning of stream and try to read 100 bytes into a BYTE buffer */
      p_inS->Seek(tmpi, STREAM_SEEK_SET, NULL);
      p_inS->Read(buf1, 100, &i);

/* Now write this same byte buffer to a second stream. Eventually I will modify this buffer, but I am leaving this
     out for the time being */
      CreateStreamOnHGlobal(NULL, TRUE, &p_outS);
      p_outS->Write(buf1, i, &j);

/* Now take the value of the new stream, and put it into a new CComBSTR object */

/* Append test value */
      out_bs2->Append("test val");

/* Return BSTR to calling object */
      *outSt2 = out_bs2->Detach();
      return S_OK;

After making the DLL, registering, and calling from my ASP page, the test function above will always return just "test val", even though I pass it a input parameter as well. Is there an easier way to convert from BSTR to BYTE array? Or is there something wrong with the above code I am using? I plan on buying a book this weekend to help me out, but if anyone could give me any tips before then I would appreciate it. Thanks.
Question by:Jeffbub

Accepted Solution

hanzelko earned 275 total points
ID: 10948306

I suppose you must add this after p_outS->Write(buf1, i, &j); :

LARGE_INTEGER li = {0, 0};
p_outS->Seek(li, STREAM_SEEK_SET, NULL);

Just seek to begin of stream and then read.
And it should work.

By the way there is an easy way to get char * or wchar * from BSTR.
and char * is the same as BYTE *.


#include <comdef.h>

BSTR h = ....  (get your BSTR from somewhere)
_bstr_t _h = _bstr_t(h);       // this will create _bstr_t wrapper from BSTR with your BSTR
char *ch = (char *)_h;         // this will return you const char* (so const pointer to one byte chars)


wchar_t *ch = (wchar_t *)_h;   // this will return you const wchar_t * (so pointer to two byte chars)

char * extractor is UNICODE safe. It does conversions from UNICODE to ANSI by calling WideCharToMultiByte.

Then you can copy it to some buffer and change it like you want.

And then if you want to return BSTR to outer world. just do:

char *out = "Some bytes";
_bstr_t _ret = _bstr_t(out);      // construct _bstr_t form char * (you can use wchar_t too)
BSTR ret = _ret.copy();           // this will return you copied BSTR from your _bstr_t

and this pointer can be returned to the world outside.

So that's it.


Author Comment

ID: 10949360
Fantastic! That worked like a charm. Thanks for the well detailed instructions!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

910 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

24 Experts available now in Live!

Get 1:1 Help Now