The Wayback Machine - https://web.archive.org/web/20090928014053/http://www.codeguru.com:80/cpp/com-tech/activex/misc/article.php/c2567/

CodeGuru
Earthweb Search
Login Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> COM-based Technologies >> ActiveX Programming >> Misc.


A Simple SafeArray Wrapper
Rating: none

Praveen S. Kumar (view profile)
July 18, 1999

|

The CSafeArray class is a wrapper for the SAFEARRAY datatype. The encapsulated member can be initialized using the Init function or using the constructor which takes the number of dimensions and the array of bounds information.


(continued)




HRESULT Init(long lnoDims, VARENUM vt, long* lBounds)

The first parameter is the number of dimensions of the array which is to be created. The second parameter is the array element type. It can be any of the variant types like VT_BSTR, VT_VARIANT etc. The third parameter is used to pass in the SAFEARRAYBOUND information for all the dimension. For eg. if a 2 dimensional array with 2 rows and 3 cols has to be created, the lBounds array should be of size 4.

long lBounds[4] = {0, 3, 0, 2} // LBound and cElements info for each dimension.

Notice that the column information is given first. Crazy safearrays take the dimensions in the reverse order. The least significant dimension has to be given last.

Once the array is created using either the constructor or the Init funtion, the array elements can be retrieved or set using the following functions

HRESULT GetElement(long* lIndex, VARIANT* pvtElement)

HRESULT SetElement(long* lIndex, VARIANT vtElement)

Again when specifying the lIndex array, the least significant dimension should be given last.

eg For accessing the element in the 2nd row and 3rd column of a 2 dimensional array, lIndex should be {2, 1}.

The number of elements, the lowerbound or upper bound in any dimension can be retreived using the following functions.

long GetElementCount(long lDim)

long GetLowerBound(long lDim) and

long GetUpperBound(long lDim).

The function ReDimArray calls ::SafeArrayReDim internally. Note that only the least significant dimension can be redimensioned. The parameters are respectively the lBound and cElements for the least significant dimension.

HRESULT ReDimArray(long lLBound, long lcElements)

The chunk of memory allocated for the array can be accessed using the member funtion

HRESULT AccessData(void HUGEP ** ppData), make sure to call the counter function after use to prevent unnecessary memory locking

HRESULT UnAccessData().

The encapsulated array member can be reteived using the function GetSafeArray

SAFEARRAY* pArr = NULL;

SafeArrObj.GetSafeArray(&pArr);

Hope this class saves a little time with safearrays.

Concern : Tested only with 2 dimensional arrays.. But should work for arrays of any dimension.

Download source - 2 KB

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
COleSafeArray::AccessData() - Legacy CodeGuru (11/24/2002)
MFC COleSafeArray Port to ATL - Legacy CodeGuru (02/17/2001)
Advantages over COleSafeArray - Legacy CodeGuru (07/21/1999)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.commediabistro.comJusttechjobs.comGraphics.com

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs