Click here to Skip to main content
13,458,661 members
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 12 Apr 2001

CBSTRStream - A simple BSTR stream implementation

, 12 Apr 2001
Rate this:
Please Sign up or sign in to vote.
CBSTRStream is a simple BSTR stream implemenation with some useful data type conversion functions.
<!-- Download Links --> <!-- Add the rest of your HTML here -->


There are several classes available that handle BSTR strings, however I have yet to find one that offers basic buffering support. The CBSTRStream class was designed to provide this feature as well as some useful data type conversion functions.

This class is not a replacement for the popular _bstr_t class, but it is designed to interoperate with it.


This class is very easy to use (as the examples below will show you). You simply use the << operator to append data to the stream. The list at the end of the article provides a more detailed overview.

Example 1 (basic usage):

_bstr_t bstrData(L"somedata");
_bstr_t bstrData2(L"some other data");
GUID guid = //someguid..
long nData = 3;
CBSTRStream bsData;
bsData << bstrData << L"TEST" << nData << L"  " << bstrData2 << L"  " << guid;

Example 2 (composing SQL queries):

long nItemId = 1;
CBSTRStream bsSelect;
bsSelect << L"SELECT It_Name, It_Desc FROM It_Item WHERE It_Id = " << nItemId;

Example 3 (using allocation hints):

long nData = 14;
long nData2 = 18;
long nData3 = 25;

CBSTRStream bsData(10, 5);
bsData << L"Numbers: " << nData << L", " << nData2;
bsData << L", " << nData3;


CBSTRStream(const CBSTRStream& bsSrc, long nInitialBufferSize = 50, long nChunkSize = 50)
CBSTRStream(long nInitialBufferSize = 50, long nChunkSize = 50)
These constructors provide you with the opportunity to set allocation hints for the stream allocator. The nInitialBufferSize sets the size used during the first buffer allocation and the nChunkSize sets the size appended during the next buffer reallocations.


CBSTRStream& operator << (const _bstr_t& bstrData)
CBSTRStream& operator << (wchar_t* pstr)
CBSTRStream& operator << (REFGUID src)
CBSTRStream& operator << (long nData)
CBSTRStream& operator << (unsigned long nData)
CBSTRStream& operator << (int nData)
CBSTRStream& operator << (double dValue)
CBSTRStream& operator << (const CBSTRStream& bsSrc)
These operators provide basic stream support as well as data type conversions. They will append the string representation of their respective data type to the stream.
operator ==
Compares two CBSTRStream objects.
operator =
Assigns a new value to an existing CBSTRStream object.
operator BSTR()
operator _bstr_t()
Extracts the pointers/objects to the encapsulated BSTR object.


void reset
Resets the string (but keeps the buffer).
long length const
Returns the length of the encapsulated BSTR.


  • This class is NOT threadsafe.


v1.0 (2001-04-13)

  • Initial Release


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Morten Abrahamsen is a software architect living in Oslo, Norway. He is currently designing and developing a commerce platform using Microsoft .Net technologies. He has extensive experience with the .Net Framework, COM+, SQL Server 2000, Visual C++/ATL and Visual C#.

You may also be interested in...

Comments and Discussions

Generala BSTR initialization Pin
bogdan27-Sep-01 0:22
memberbogdan27-Sep-01 0:22 
GeneralBSTR Pin
Anonymous28-Jun-01 3:47
memberAnonymous28-Jun-01 3:47 
GeneralRe: BSTR Pin
Anonymous10-Jul-01 5:48
memberAnonymous10-Jul-01 5:48 
GeneralRe: BSTR Pin
Morten Abrahamsen10-Mar-02 1:47
memberMorten Abrahamsen10-Mar-02 1:47 
No, it's a buffered string builder (hence the stream).

It offers much higher performance than CComBSTR.
(plus a some nice overloads)Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180323.1 | Last Updated 13 Apr 2001
Article Copyright 2001 by Morten Abrahamsen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid