Posts Tagged ‘COleSafeArray’

How to create SafeArray of BSTR’s?

June 22, 2007 5 comments

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: , , ,

Creating SafeArray of bytes.

How to create a safearray of bytes…

// An array having some data
unsigned char ucBytes[2048]; 

// Create ole safe array object 
COleSafeArray cosaByteArray;

// Copy bytes to safe array
cosaByteArray.CreateOneDim( VT_UI1, sizeof( ucBytes ), ucBytes );

// Get safe array in variant form
_variant_t vtByteArray = cosaByteArray.Detach();
%d bloggers like this: