BSTR to BYTE problem
Posted on 2004-04-29
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)
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 */
/* Return BSTR to calling object */
*outSt2 = out_bs2->Detach();
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.