Click to See Complete Forum and Search --> : BSTR Blues


Simon Rose
May 19th, 1999, 04:04 PM
Hi,

This may be a beginner question, but I am having problems. I am creating my first, so simple, ATL COM object with MFC support in VC 5.0. I have methods and properties which as in/out parameters utilize BSTR FAR* as strings (because I cannot use CString for VB to work etc.

1. First problem is converting BSTR FAR* to a CString (it just become garbage)
2. Setting a BSTR FAR* value as an out parameter for properties from a CString. I used AllocSysString casted to BSTR * but it filled the BSTR with garbage.
3. Similarly, all my long * parameters are suffering the same fate (garbage).

Can anyone tell me how to do parameter passing where the values are not garbled ? Should I be using VARIANT and VARIANTARG ? If so how ?

Thanks for any help in advance,

Simon

Cosmin
May 19th, 1999, 04:10 PM
Hi,

Try this:
1. CString can be constructed (with the constructor!) getting a BSTR parameter. Write something like this:

CString str;
BSTR bstr;
str = CString(bstr);
...

2. You should make a copy of what AllocSysString returns (allocate space and copy).

Hope it works!

CBurfoot
May 19th, 1999, 04:57 PM
Watch out for [out] IDL notation.

You'll need to pass addres of placeholder types.

BSTR bname = NULL;

// from CString

bname = cstringname.AllocSysString();

psecuredptr->Login( bname );


NOTE: for a return value

BSTR bname = NULL;

psomeCOMptr->GetBstrval( &bname );

CString name = bname;


Like wise of long's
long lval;
psomeBinCOMCall->GetLong( &lval );


This '&' (address of operator) means a location
in memory to place the result.

Try it.

CBurfoot
May 19th, 1999, 05:00 PM
OOPPS:

When using CSTRING::AllocSysString()
after using the BSTR, SysFreeString( bstr );

Can use a CComVariant more easily for [in] params

SomeClientApp::Login( CString& name )
{
CComVariant var;
var = name;
psecurity->Login( var.bstrVal );
// No need to clean up the bstr, the CComVariant does that automatically

}