Home > COM > How to create SafeArray of BSTR’s?

How to create SafeArray of BSTR’s?

Here is a sample to create an array of BSTR’s…

const long lCount = 100;
_bstr_t bstrArray[lCount]; 

COleSafeArray cosaSafeArray;
cosaSafeArray.CreateOneDim( VT_BSTR, lCount ) 

// Fill out this array
for( long lIndex = 0; lIndex < lCount; ++lIndex )
  cosaSafeArray.PutElement( &lIndex,
			 static_cast<BSTR>( bstrArray[lIndex] );

// Now you have a safe array of bstr's and since COleSafeArray
// is inherited from VARIANT you can directly use this safearray
// in COM calls with no overhead for freeing safe array
Categories: COM Tags: , , ,
  1. No way
    March 9, 2009 at 3:15 pm

    Use CComSafeArray instead as it exists in ATL and don’t require MFC.

  2. March 11, 2008 at 5:27 am

    Hey Ajay, nice to read your comment, Thanks!

  3. Ajayraj
    March 11, 2008 at 5:16 am

    Good method man.!

  4. February 26, 2008 at 10:20 am

    Nice to hear that 🙂

  5. Rianna
    February 26, 2008 at 10:16 am

    Thank you for this simple info…really helped!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: