Board index » cppbuilder » BSTRs in an interface

BSTRs in an interface

I am using a third-party OCX developed using VC. I am the first person to
try to use their OCX with Builder. Their interface takes 8 byte char strings
as parameters, but when I import the OCX, the generated include file uses
BSTRs for all the char * parameters. I tried using L"StringValue", but this
produces an access violation in their code. I also tried just casting the
pointers to (wchar_t *) to shut up the compiler, and this produces a pair of
debug traps for each string I pass. The routine returns success, but doesn't
actually do anything. Does anyone know why this should be?

Thanks,

Don Wilcox
dwil...@dfsaz.com

 

Re:BSTRs in an interface


Don,
I am surprised nobody has respond yet, so...

First, read up on VCL class WideString.
Sample usage:

  BSTR  bstr;
  WideString wstr = "abc";
  bstr = wstr;

gl...

Tom

Quote
Don Wilcox <dwil...@dfsaz.com> wrote in message news:3b55e437$1_2@dnews...
> I am using a third-party OCX developed using VC. I am the first person to
> try to use their OCX with Builder. Their interface takes 8 byte char
strings
> as parameters, but when I import the OCX, the generated include file uses
> BSTRs for all the char * parameters. I tried using L"StringValue", but
this
> produces an access violation in their code. I also tried just casting the
> pointers to (wchar_t *) to shut up the compiler, and this produces a pair
of
> debug traps for each string I pass. The routine returns success, but
doesn't
> actually do anything. Does anyone know why this should be?

> Thanks,

> Don Wilcox
> dwil...@dfsaz.com

Re:BSTRs in an interface


i dont think that's the way to use BSTRs cause this way is way to memory
leaks
use SysAllocString to alloc a bstr from a  WideString
and SysFreeString to Free the BSTR after using it
sampl::
BSTR zog;
zog=SysAllocString(WideString("thestring").c_bstr());
//use zog
SysFreeString(zog);

Monq

Tom K. <tk...@socal.rr.com> a crit dans le message : 3b567d42$1_2@dnews...

Quote
> Don,
> I am surprised nobody has respond yet, so...

> First, read up on VCL class WideString.
> Sample usage:

>   BSTR  bstr;
>   WideString wstr = "abc";
>   bstr = wstr;

> gl...

> Tom

> Don Wilcox <dwil...@dfsaz.com> wrote in message news:3b55e437$1_2@dnews...
> > I am using a third-party OCX developed using VC. I am the first person
to
> > try to use their OCX with Builder. Their interface takes 8 byte char
> strings
> > as parameters, but when I import the OCX, the generated include file
uses
> > BSTRs for all the char * parameters. I tried using L"StringValue", but
> this
> > produces an access violation in their code. I also tried just casting
the
> > pointers to (wchar_t *) to shut up the compiler, and this produces a
pair
> of
> > debug traps for each string I pass. The routine returns success, but
> doesn't
> > actually do anything. Does anyone know why this should be?

> > Thanks,

> > Don Wilcox
> > dwil...@dfsaz.com

Re:BSTRs in an interface


i dont think that's the way to use BSTRs cause this way is way to memory
leaks
use SysAllocString to alloc a bstr from a  WideString
and SysFreeString to Free the BSTR after using it
Monq
BSTR zog;
zog=SysAllocString(WideString("thestring").c_bstr());
//use zog
SysFreeString(zog);

Tom K. <tk...@socal.rr.com> a crit dans le message : 3b567d42$1_2@dnews...

Quote
> Don,
> I am surprised nobody has respond yet, so...

> First, read up on VCL class WideString.
> Sample usage:

>   BSTR  bstr;
>   WideString wstr = "abc";
>   bstr = wstr;

> gl...

> Tom

> Don Wilcox <dwil...@dfsaz.com> wrote in message news:3b55e437$1_2@dnews...
> > I am using a third-party OCX developed using VC. I am the first person
to
> > try to use their OCX with Builder. Their interface takes 8 byte char
> strings
> > as parameters, but when I import the OCX, the generated include file
uses
> > BSTRs for all the char * parameters. I tried using L"StringValue", but
> this
> > produces an access violation in their code. I also tried just casting
the
> > pointers to (wchar_t *) to shut up the compiler, and this produces a
pair
> of
> > debug traps for each string I pass. The routine returns success, but
> doesn't
> > actually do anything. Does anyone know why this should be?

> > Thanks,

> > Don Wilcox
> > dwil...@dfsaz.com

Re:BSTRs in an interface


Thanks,

This worked. I haven't checked for memory leaks to see if the other solution
is preferred.

Don

Quote
"Tom K." <tk...@socal.rr.com> wrote in message news:3b567d42$1_2@dnews...
> Don,
> I am surprised nobody has respond yet, so...

> First, read up on VCL class WideString.
> Sample usage:

>   BSTR  bstr;
>   WideString wstr = "abc";
>   bstr = wstr;

> gl...

> Tom

> Don Wilcox <dwil...@dfsaz.com> wrote in message news:3b55e437$1_2@dnews...
> > I am using a third-party OCX developed using VC. I am the first person
to
> > try to use their OCX with Builder. Their interface takes 8 byte char
> strings
> > as parameters, but when I import the OCX, the generated include file
uses
> > BSTRs for all the char * parameters. I tried using L"StringValue", but
> this
> > produces an access violation in their code. I also tried just casting
the
> > pointers to (wchar_t *) to shut up the compiler, and this produces a
pair
> of
> > debug traps for each string I pass. The routine returns success, but
> doesn't
> > actually do anything. Does anyone know why this should be?

> > Thanks,

> > Don Wilcox
> > dwil...@dfsaz.com

Re:BSTRs in an interface


Don,

Depending on how you are using the BSTR, you may want to detach the bstr
from the widestring, so the BSTR takes ownership:

BSTR  bstr;
WideString wstr = "abc";
bstr = wstr.Detach();

Re:BSTRs in an interface


I am passing them into an ocx as read-only input parameters. I am not
currently detaching them, an dam experiencing no problems. What are the
rules on detaching?

Re:BSTRs in an interface


surly work but that must be very carrfull with the attach and detach cause
using them is very specific (returning a result by val for exemple)
otherwise for what you want to do the two ways work(dont forget to free the
BSTR in the 2 ways specialy in BCB5 cause the ATL impl is the source of to
much memory leaks you can find a good note about this at the Borland
community (if you dont find it ill send it to you by mail)!!
Monq

Steve Cox <steve....@datria.com> a crit dans le message :
3b570fc9_1@dnews...

Quote
> Don,

> Depending on how you are using the BSTR, you may want to detach the bstr
> from the widestring, so the BSTR takes ownership:

> BSTR  bstr;
> WideString wstr = "abc";
> bstr = wstr.Detach();

Re:BSTRs in an interface


Quote
"Maloum.m" <m.mal...@midinov.com> wrote in message news:3b569043_2@dnews...
> i dont think that's the way to use BSTRs cause this way is way to memory
leaks
> use SysAllocString to alloc a bstr from a  WideString
> and SysFreeString to Free the BSTR after using it
> sampl::
> BSTR zog;
> zog=SysAllocString(WideString("thestring").c_bstr());
> //use zog
> SysFreeString(zog);

Simpler to use Detach() on the widestring

Re:BSTRs in an interface


On Borland's CodeCentral, there is a document named COMNotes.doc From Phil
Parker that talks about how to use the WideString class.  If you are passing
a WideString as in [in] parameter to a COM object, you are probably OK not
doing the detach.

If your BSTR ever gets wrapped in a VCL Variant (which happens in BCB 3 when
calling dispatch interfaces in the ActiveX Wrapper classes), you will need
to Detach the WideString, because the Variant thinks it owns it and will
delete it.  If you don't detach it will be attempted to be deleted twice.

The following is from the COMNotes document:

operator BSTR() const

The above operator will act as a cast operator for WideStrings where a BSTR
is expected, e.g. if a BSTR is expected as an argument for a function and
you pass a WideString, this cast operator will be called to convert the
WideString to a BSTR.  The operator will return a handle to the BSTR string
owned by the WideString.  Therefore, care must be taken that incorrect
alteration of the string does not take place.  This operator is commonly
used to enable transparent passing of WideStrings as [in] parameters for COM
methods that expect a BSTR argument.

Quote
"Steve Cox" <steve....@datria.com> wrote in message news:3b570fc9_1@dnews...
> Don,

> Depending on how you are using the BSTR, you may want to detach the bstr
> from the widestring, so the BSTR takes ownership:

> BSTR  bstr;
> WideString wstr = "abc";
> bstr = wstr.Detach();

Other Threads