Using BSTR 
Author Message
 Using BSTR

I have done some ATL/C++ coming from VB background. In my ATL component I
need to setup a constant, add or append the const to some dynamically built
strings and send the string to ADO object for execution. If someone could
give a simple code snippet that shows the best approach for this or send me
to a site that explains this. I have worked with BSTR's in my programs but
not with constants.

Thanks in advance...



Fri, 29 Apr 2005 02:20:47 GMT  
 Using BSTR
A constant what? string?

This really is a C++ question. Why not use:

const wchar_t MyConstantString[] = L"Hello";

If you have a BSTR called bstrInput, then to append:

CComBSTR b (bstrInput);
b += MyConstantString;

Brian Muth



Fri, 29 Apr 2005 04:05:55 GMT  
 Using BSTR
Thanks Brian.


Quote:
> A constant what? string?

> This really is a C++ question. Why not use:

> const wchar_t MyConstantString[] = L"Hello";

> If you have a BSTR called bstrInput, then to append:

> CComBSTR b (bstrInput);
> b += MyConstantString;

> Brian Muth



Fri, 29 Apr 2005 05:44:37 GMT  
 Using BSTR
Sorry, I meant:

b += MyConstantString;


Quote:
> A constant what? string?

> This really is a C question. Why not use:

> const wchar_t MyConstantString[] = L"Hello";

> If you have a BSTR called bstrInput, then to append:

> CComBSTR b (bstrInput);
> b = MyConstantString;

> Brian Muth



Fri, 29 Apr 2005 05:48:49 GMT  
 Using BSTR
In place of using CComBSTR, you could use the COM support class _bstr_t. In
general, the COM support classes include _variant_t, _bstr_t, and _com_ptr_t
which is the smart pointer used by #import. Just include comdef.h.

Here is who something would look

wchar_t myConstant = L"Hello World";
int myIntConstant = 5;

_bstr_t bstrTest(L"I just wanted to say, ");
bstrTest += myConstant + L" " + _itow(myIntConstant) + L" times";

I typically use _bstr_t and _variant_t because they are always present when
you do #import and you can use them in all other situations. I'm also
switching from CComPtr to _com_ptr_t for that reason. I like one way of
doing things. The above also works for ascii strings. Just change the _itow
to _itoa and drop the Ls. Also, switch wchar_t to char.


Quote:
> Thanks Brian.



> > A constant what? string?

> > This really is a C++ question. Why not use:

> > const wchar_t MyConstantString[] = L"Hello";

> > If you have a BSTR called bstrInput, then to append:

> > CComBSTR b (bstrInput);
> > b += MyConstantString;

> > Brian Muth



Fri, 29 Apr 2005 10:55:22 GMT  
 Using BSTR
Thanks Sheikh


Quote:
> In place of using CComBSTR, you could use the COM support class _bstr_t.
In
> general, the COM support classes include _variant_t, _bstr_t, and
_com_ptr_t
> which is the smart pointer used by #import. Just include comdef.h.

> Here is who something would look

> wchar_t myConstant = L"Hello World";
> int myIntConstant = 5;

> _bstr_t bstrTest(L"I just wanted to say, ");
> bstrTest += myConstant + L" " + _itow(myIntConstant) + L" times";

> I typically use _bstr_t and _variant_t because they are always present
when
> you do #import and you can use them in all other situations. I'm also
> switching from CComPtr to _com_ptr_t for that reason. I like one way of
> doing things. The above also works for ascii strings. Just change the
_itow
> to _itoa and drop the Ls. Also, switch wchar_t to char.



> > Thanks Brian.



> > > A constant what? string?

> > > This really is a C++ question. Why not use:

> > > const wchar_t MyConstantString[] = L"Hello";

> > > If you have a BSTR called bstrInput, then to append:

> > > CComBSTR b (bstrInput);
> > > b += MyConstantString;

> > > Brian Muth



Fri, 29 Apr 2005 11:51:28 GMT  
 Using BSTR

Quote:

> In place of using CComBSTR, you could use the COM support class _bstr_t. In
> general, the COM support classes include _variant_t, _bstr_t, and _com_ptr_t
> which is the smart pointer used by #import. Just include comdef.h.

If you're using ATL, there's no particular reason to avoid using
CComBSTR.  It's best to just use the one whose interface you prefer.

--
Craig Powers
MVP - Visual C++



Fri, 29 Apr 2005 22:49:20 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Using BSTR in COM Exe server

2. using BSTR

3. Using BSTR and CString

4. Using BSTR

5. Using BSTR.

6. Using BSTR in normal eVC dll and calling from eVB

7. BSTR BSTR and BSTR

8. Using, in CE, _bstr_t (the BSTR COM encapsulation)

9. Freeing a BSTR when using SafeArray{Get|Put}Element

10. Using VB-DLL functions (VGAP4-DLL) and the datatype BSTR

11. Using, in CE, _bstr_t (the BSTR COM encapsulation)

12. BSTR concatenation and BSTR memory freeing

 

 
Powered by phpBB® Forum Software