Click here to Skip to main content
Click here to Skip to main content

Simplify your Safearray loops using macros

, 15 Nov 2001
Rate this:
Please Sign up or sign in to vote.
An article describing how to simplify the work with safearrays

The purpose of this article is show how you can simplify your safearray programming in VC++ and how to make the code even more readable. The article will briefly define safearrays and the introduce a new set of macros and how you can use them.


What are Safearrays? As it's name says it is an array data type that is safe wich means that it keeps track of it's limitations and bounds and therefore limits access within these bounds. Safearrays are used when passing arrays encapsulated in Variants (their base type is VT_? | VT_ARRAY), for example between COM objects.

Safearrays must not be confused with C/C++ arrays which are not safe. When working with Visual Basic COM objects and Visual Basic arrays your only choice is to learn how to use Safearrays, MSDN is a good source for more information on this topic (or perhaps a more extended article in the future Smile | :) .

How to loop through a Safearray

Those of you familiar with Safearrays knows that it takes quite a large number of code-rows to iterate through a Safearray. The example below [sample 1] shows how the code looks like whe iterating through a Safearray containing Variants.

// Safearray loop [sample 1]
// this example assumes that you have a Safearray in a VARIANT (vArray) 
// with the type VT_ARRAY | VT_BSTR

// variables
LONG lstart, lend;
LONG idx = -1;
LONG nPos;
BSTR* pbstr;

// assign the Safearray
SAFEARRAY *sa = V_ARRAY( &vArray ); 

// Get the lower and upper bound
hr = SafeArrayGetLBound( sa, 1, &lstart );
if(FAILED(hr)) return hr;
hr = SafeArrayGetUBound( sa, 1, &lend );
if(FAILED(hr)) return hr;

// loop
hr = SafeArrayAccessData(sa,(void HUGEP**)&pbstr);
	for(idx=lstart; idx <= lend; idx++)
		CComBSTR s;
		s = pbstr[idx];
		// s now contains the item at position idx in the array
		// ...		
	hr = SafeArrayUnaccessData(sa);	
	if(FAILED(hr)) return hr;
	return hr
// (23 lines of code)

Notice the amount of code, imagine if we could reduce this to 8 lines and think of how much easier your code would be to interpret and for others to understand.

Tip! Also notice how we use the SafeArrayAccessData to access the data in the loop instead of using SafeArrayGetElement to improve performance.

How to loop the Safearray using the macros

To make my code easier to read and make the programming faster I created a set of macros to make the Safearray programming easier. Let's see the same example as above [sample 1] using macros [sample 2].

<PRE>// Safearray loop [sample 2]
// this example assumes that you have a Safearray in a VARIANT (vArray) with 
// the type VT_ARRAY | VT_BSTR

#include "safearray_macro.h"

// variables
BSTR* pbstr;

// loop
        CComBSTR s;
        s = pbstr[INDEX_SA];
        // s now contains the item at the current position in the array
        // ...        
// (8 lines of code)
Easy, huh? Just an include file and a new set of macros...

Let's see what the different macros are doing...

Macro definitions

BEGIN_SA_V(vArray, pvar)
Starts the macro section and defines the local variables needed. vArray is a Variant containg the Safearray and pvar is the variable used in the loop for each item in the array.

BEGIN_SA(sa, pvar)
Starts the macro section and defines the local variables needed. sa is a Safearray and pvar is the variable used in the loop for each item in the array.

Ends the macro section.

Starts the loop. The INDEX_SA is the iterator variable and you can access the current item using pvar[INDEX_SA].

The end of the loop.

Is the iterator variable inside the loop (the type is LONG).

Can be used in the actual loop to break it, as long as it doesn't appear in a nestef for-loop.

If you plan to exit the method inside the loop you should use this macro to be sure that the Safearray is closed.

This macro can be used if you would like to access the SAFEARRAY object directly. Useful when the Safearray existed in a Variant.


To test the macros test the included VC++ and VB projects, which illustrates both the BEGIN_SA_V and BEGIN_SA macros.

Source and Usage

Just include, #include "safearray_macro.h", the file in your code and start using it. If make any extensions/modifications to it please notify me!

Have fun...


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

Wictor Wilén
Web Developer
Sweden Sweden
No Biography provided

Comments and Discussions

GeneralGood Exercise done on SAFEARRAY PinmemberMember 18700681-Jul-10 5:59 
Generalhelp in two dimensional SAFEARRAY Pinmemberrasred4-Sep-08 2:40 
QuestionMultiDim Pinmembercjk0070019-Oct-06 7:35 
AnswerRe: MultiDim Pinmemberfrewah28-Jul-07 14:20 
GeneralVT_BSTR|VT_ARRAY to WCHAR* Pinmemberdharani14-Sep-04 1:11 
Generalstructure Pinmemberlioucr17-Nov-01 18:33 
GeneralGood job for beginner! PinmemberAnonymous4-Aug-01 1:30 
GeneralCOleSafeArray PinmemberAnonymous31-Jul-01 23:34 
GeneralRe: COleSafeArray PinmemberWictor Wilén31-Jul-01 23:48 
GeneralMFC COleSafeArray port to ATL PinmemberAnonymous1-Aug-01 4:42 
GeneralRe: COleSafeArray PinsussAnonymous2-Dec-02 10:50 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 16 Nov 2001
Article Copyright 2001 by Wictor Wilén
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid