Help Center
Help CenterAfxNovaCOM

DSAFEARRAY Class

Members (64)

AfxStrJoinReturns a string consisting of all of the strings in an array, each separated by a delimiter.AfxStrSplitSplits a string into tokens.AccessDataIncrements the lock count of an array, and retrieves a pointer to the array data.AppendAppends a value to the end of the one-dimensional safe array.AppendStrAppendVarAttachAttaches a safe array to a DSAFEARRAY object.ClearLike DestroyData, destroys all the data in a safe array. It is the same as Erase and Reset.ConstructorCreates a DSAFEARRAY.ConstructorCreates a DSAFEARRAY.ConstructorCreates a DSAFEARRAY.ConstructorCreates a DSAFEARRAY.CopyCreates a copy of the safe array.CopyDataCopies the source array to the target array after releasing any resources in the target array.CopyFromCopies the contents of a safe array.CopyFromVariantCopies the contents of a VARIANT of type VT_ARRAY to the object.CopyToVariantCopies the safe array to the passed variant.CountReturns the number of elements in the specified dimension of the array.CreateCreates a safe array from the given VARTYPE, number of dimensions and bounds.CreateExCreates a safe array from the given VARTYPE, number of dimensions and bounds.CreateVectorCreates a one-dimensional safe array from the given VARTYPE, lower bound and number elements.CreateVectorExCreates a one-dimensional safe array from the given VARTYPE, lower bound and number elements.DestroyDestroys an existing array descriptor and all of the data in the array.DestroyDataDestroys all the data in a safe array.DetachDetaches the sage array descriptor from the DSAFEARRAY.ElemSizeReturns the size of an element.EraseLike DestroyData, destroys all the data in a safe array. It is the same as Clear and Reset.FeaturesReturns the flags used by the safe array. This is the same that the Flags method.FindScans the array to search for the specified string.GetRetrieves a single element of the array.GetIIDReturns the GUID of the interface contained within a given safe array.GetPtrReturns the address of the safe array.GetRecordInfoRetrieves the IRecordInfo interface of a UDT contained in a given safe array.GetStrGetTypeReturns the VARTYPE stored in the given safe array.GetVarInsertInserts a value at the specified position of the safe array.InsertStrInsertVarIsResizableTests if the safe array can be resized.LBoundReturns the lower bound for any dimension of a safe array.LocksCountReturns the number of times the array has been locked without the corresponding unlock.MoveFromVariantTransfers ownership of the safe array contained in the variant parameter to this object.MoveToVariantTransfers ownership of the safe array to a variant and detaches it from the class.NumDimsReturns the number of dimensions in the array.Operator *Returns a pointer to the safe array descriptor.Operator LETAssigns a DSAFEARRAY, a safe array or a VARIANT.PtrOfIndexReturns a pointer to an array element.PutStores the data element at a given location in the array.PutStrPutVarRedimChanges the right-most (least significant) bound of a safe array.RemoveDeletes the specified array element.RemoveStrRemoveVarResetLike DestroyData, destroys all the data in a safe array. It is the same that Clear and Erase.SAFEARRAYRepresents a safe array.SAFEARRAYBOUNDRepresents the bounds of one dimension of the array.SetIIDSets the GUID of the interface contained within a given safe array.SetRecordInfoSets the IRecordInfo interface of the UDT contained in a given safe array.SortSorts a one-dimensional VT_BSTR DSAFEARRAY calling the C qsort function.UBoundReturns the upper bound for any dimension of a safe array.UnaccessDataDecrements the lock count of an array, and invalidates the pointer retrieved by AccessData.VARTYPESafe array VARIANT type.

Documentation

DSAFEARRAY Class

DSAFEARRAY is a class that provides wrapper methods for the SAFEARRAY structure, making it easy to create and manage single- and multidimensional arrays of almost any of the VARIANT-supported types. The lower bound of a DSAFEARRAY can start at any user-defined value. Additional overloaded methods are provided for one and two-dimensional safe arrays, that are the ones most often used in COM programming.

When the class is destroyed, the array descriptor and all of the data in the array is destroyed. If objects are stored in the array, IUnknown_Release is called on each object in the array. Safe arrays of variants will have VariantClear called on each member and safe arrays of BSTR will have SysFreeString called on each element. IRecordInfo.RecordClear will be called to release object references and other values of a record without deallocating the record.

Include file: AfxNova/DSafeArray.inc.


Structures and VARTYPE

NameDescription
SAFEARRAYRepresents a safe array.
SAFEARRAYBOUNDRepresents the bounds of one dimension of the array.
VARTYPESafe array VARIANT type.

Constructors

NameDescription
Constructor (SAFEARRAYBOUND)Creates a DSAFEARRAY.
Constructor (DSAFEARRAY)Creates a DSAFEARRAY from another DSAFEARRAY.
Constructor (SAFEARRAY PTR)Creates a DSAFEARRAY from a safe array.
Constructor (VARIANT PTR)Creates a DSAFEARRAY from a VARIANT of type VT_ARRAY.

Operators

NameDescription
Operator \*Returns a pointer to the safe array descriptor.
Operator LETAssigns a DSAFEARRAY, a safe array or a VARIANT.

Methods

NameDescription
AccessDataIncrements the lock count of an array, and retrieves a pointer to the array data.
AppendAppends a value to the end of the one-dimensional safe array.
AttachAttaches a safe array to a DSAFEARRAY object.
ClearLike DestroyData, destroys all the data in a safe array. It is the same as Erase and Reset.
CopyCreates a copy of the safe array.
CopyDataCopies the source array to the target array after releasing any resources in the target array.
CopyFromCopies the contents of a safe array.
CopyFromVariantCopies the contents of a VARIANT of type VT_ARRAY to the object.
CopyToVariantCopies the safe array to the passed variant.
CountReturns the number of elements in the specified dimension of the array.
CreateCreates a safe array from the given VARTYPE, number of dimensions and bounds.
CreateExCreates a safe array from the given VARTYPE, number of dimensions and bounds.
CreateVectorCreates a one-dimensional safe array from the given VARTYPE, lower bound and number elements.
CreateVectorExCreates a one-dimensional safe array from the given VARTYPE, lower bound and number elements.
DestroyDestroys an existing array descriptor and all of the data in the array.
DestroyDataDestroys all the data in a safe array.
DetachDetaches the sage array descriptor from the DSAFEARRAY.
ElemSizeReturns the size of an element.
EraseLike DestroyData, destroys all the data in a safe array. It is the same as Clear and Reset.
FeaturesReturns the flags used by the safe array. This is the same that the Flags method.
FindScans the array to search for the specified string.
FlagsReturns the flags used by the safe array. This is the same as the Features method.
GetRetrieves a single element of the array.
GetIIDReturns the GUID of the interface contained within a given safe array.
GetPtrReturns the address of the safe array.
GetRecordInfoRetrieves the IRecordInfo interface of a UDT contained in a given safe array.
GetTypeReturns the VARTYPE stored in the given safe array.
InsertInserts a value at the specified position of the safe array.
IsResizableTests if the safe array can be resized.
LBoundReturns the lower bound for any dimension of a safe array.
LocksCountReturns the number of times the array has been locked without the corresponding unlock.
MoveFromVariantTransfers ownership of the safe array contained in the variant parameter to this object.
MoveToVariantTransfers ownership of the safe array to a variant and detaches it from the class.
NumDimsReturns the number of dimensions in the array.
PtrOfIndexReturns a pointer to an array element.
PutStores the data element at a given location in the array.
RedimChanges the right-most (least significant) bound of a safe array.
RemoveDeletes the specified array element.
ResetLike DestroyData, destroys all the data in a safe array. It is the same that Clear and Erase.
SetIIDSets the GUID of the interface contained within a given safe array.
SetRecordInfoSets the IRecordInfo interface of the UDT contained in a given safe array.
SortSorts a one-dimensional VT_BSTR DSAFEARRAY calling the C qsort function.
UBoundReturns the upper bound for any dimension of a safe array.
UnaccessDataDecrements the lock count of an array, and invalidates the pointer retrieved by AccessData.

Helper Procedures

NameDescription
AfxStrJoinReturns a string consisting of all of the strings in an array, each separated by a delimiter.
AfxStrSplitSplits a string into tokens.

SAFEARRAY Structure

Represents a safe array.

TYPE tagSAFEARRAY
   cDims as USHORT
   fFeatures as USHORT
   cbElements as ULONG
   cLocks as ULONG
   pvData as PVOID
   rgsabound(0 to 0) as SAFEARRAYBOUND
EBD TYPE
MemberDescription
cDimsCount of dimensions of the array.
fFeaturesFlags.
cbElementsSize of an element of the array.
cLocksNumber of times the array has been locked without corresponding unlock.
pvDataPointer to the data.
rgsaboundOne bound for each dimension.
fFeatures Flags
FlagDescription
FADF_AUTOAn array that is allocated on the stack.
FADF_STATICAn array that is statically allocated.
FADF_EMBEDDEDAn array that is embedded in a structure.
FADF_FIXEDSIZEAn array that may not be resized or reallocated.
FADF_RECORDAn array that contains records. When set, there will be a pointer to the IRecordinfo interface at negative offset 4 in the array descriptor.
FADF_HAVEIIDAn array that has an IID identifying interface. When set, there will be a GUID at negative offset 16 in the safe array descriptor. Flag is set only when FADF_DISPATCH or FADF_UNKNOWN is also set.
FADF_HAVEVARTYPEAn array that has a VT type. When set, there will be a VT tag at negative offset 4 in the array descriptor that specifies the element type.
FADF_BSTRAn array of BSTRs.
FADF_UNKNOWNAn array of IUnknown pointers.
FADF_DISPATCHAn array of IDispatch pointers.
FADF_VARIANTAn array of VARIANTs.
FADF_RESERVEDBits reserved for future use.
Remarks

The array rgsabound is stored with the left-most dimension in rgsabound[0] and the right-most dimension in rgsabound(cDims - 1).

The fFeatures flags describe attributes of an array that can affect how the array is released. The fFeatures field describes what type of data is stored in the SAFEARRAY and how the array is allocated. This allows freeing the array without referencing its containing variant. The bits are accessed using the following constants:

Thread Safety

All public static members of the SAFEARRAY data type are thread safe. Instance members are not guaranteed to be thread safe.

For example, consider an application that uses the SafeArrayLock and SafeArrayUnlock functions. If these functions are called concurrently from different threads on the same SAFEARRAY data type instance, an inconsistent lock count may be created. This will eventually cause the SafeArrayUnlock function to return E_UNEXPECTED. You can prevent this by providing your own synchronization code.


SAFEARRAYBOUND Structure

Represents the bounds of one dimension of the array. The lower bound of the dimension is represented by lLbound, and cElements represents the number of elements in the dimension. The structure is defined as follows:

TYPE SAFEARRAYBOUND
   cElements as ULONG
   lLbound as LONG
END TYPE
MemberDescription
cElementsNumber of elements in the dimension.
lLboundThe lower bound of the dimension.

Safe array Variant type

The base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.

VarTypeMeaningData typestrType
VT_BSTRUnicode stringBSTR"BSTR"
VT_I1Signed byteBYTE"BYTE"
VT_UI1Unsigned byteUBYTE"UBYTE"
VT_I2Signed shortSHORT"SHORT"
VT_UI2Unsigned shortUSHORT"USHORT"
VT_I4Signed longLONG"LONG"
VT_INTSigned longLONG"ULONG"
VT_UI4Unsigned longULONG"LONG"
VT_UINTUnsigned longULONG"ULONG"
VT_I8Signed quadLONGINT"LONGINT"
VT_UI8Unnsigned quadULONGINT"ULONGINT"
VT_R4SingleSINGLE"SINGLE"
VT_R8DoubleDOUBLE"DOUBLE"
VT_CURCurrencyCY"CURRENCY"
VT_BOOLBoolean (cast to a signed short)SHORT"BOOL"
VT_DATEDateDATE_"DATE"
VT_DECIMALDecimal structureDECIMAL"DECIMAL"
VT_VARIANTVariantVARIANT"VARIANT"
VT_UNKNOWNIUnknown pointerIUnknown PTR"UNKNOWN"
VT_DISPATCHIDispatch pointerIDispatch PTR"DISPATCH"

Constructor (SAFEARRAYBOUND)

Creates a DSAFEARRAY.

Multidimensional array:

CONSTRUCTOR DSAFEARRAY (BYVAL vt AS VARTYPE, BYVAL cDims AS UINT, BYVAL prgsabounds AS SAFEARRAYBOUND PTR)
CONSTRUCTOR DSAFEARRAY (BYREF strType AS STRING, BYVAL cDims AS UINT, BYVAL prgsabounds AS SAFEARRAYBOUND PTR)
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.
strTypeThe base type of the array as a string literal.
cDimsNumber of dimensions in the array. The number cannot be changed after the array is created.
rgsaboundPointer to a vector of bounds (one for each dimension) to allocate for the array.

One-dimensional array:

CONSTRUCTOR DSAFEARRAY (BYVAL vt AS VARTYPE, BYVAL cElements AS ULONG = 0, BYVAL lLBound AS LONG = 0)
CONSTRUCTOR (BYREF strType AS STRING, BYVAL cElements AS ULONG = 0, BYVAL lLBound AS LONG = 0)
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.
strTypeThe base type of the array as a string literal.
cElementsOptional. Number of elements in the array.
lLBoundOptional. The lower bound of the array.

Two-dimensional array:

CONSTRUCTOR DSAFEARRAY (BYVAL vt AS VARTYPE, BYVAL cElements1 AS ULONG, BYVAL lLBound1 AS LONG, _
   BYVAL cElements2 AS ULONG, BYVAL lLBound2 AS LONG)
CONSTRUCTOR (BYREF strType AS STRING, BYVAL cElements1 AS ULONG, BYVAL lLBound1 AS LONG, _
   BYVAL cElements2 AS ULONG, BYVAL lLBound2 AS LONG)
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.
strTypeThe base type of the array as a string literal.
cElements1Number of elements in the first dimension of the array.
lLBound1The lower bound of the first dimension of the array.
cElements2Number of elements in the second dimension of the array.
lLBound2The lower bound of the second dimension of the array.
Usage examples
' // Two-dimensional array of BSTR
' // 2D: elements = 5, lower bound = 1
' // 2D: elements = 3, lower bound = 1
DIM rgsabounds(0 TO 1) AS SAFEARRAYBOUND = {(5, 1), (3, 1)}
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_BSTR, 2, @rgsabounds(0))
-or-
' // Two-dimensional array of BSTR
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_BSTR, 5, 1, 3, 1)

' // One-dimensional array of VT_VARIANT with 0 elements and a lower-bound of 0
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_VARIANT, 0, 0)
-or-
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_VARIANT)

' // One-dimensional array of VT_BSTR with 5 elements and a lower-bound of 1
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_BSTR, 5, 1)

Constructor (SAFEARRAYBOUND)

Creates a DSAFEARRAY from another DSAFEARRAY.

CONSTRUCTOR DSAFEARRAY (BYREF dsa AS DSAFEARRAY)
ParameterDescription
dsaA DSAFEARRAY object.

Constructor (SAFEARRAY PTR)

Creates a DSAFEARRAY from a safe array.

CONSTRUCTOR DSAFEARRAY (BYVAL psa AS SAFEARRAY PTR)
CONSTRUCTOR DSAFEARRAY (BYVAL psa AS SAFEARRAY PTR, BYVAL fAttach AS BOOLEAN)
ParameterDescription
psaPointer to a safe array.
fAttachIf TRUE, the safe array is attached, else a copy is made.

Constructor (VARIANT PTR)

Creates a DSAFEARRAY from a Variant of type VT_ARRAY.

CONSTRUCTOR DSAFEARRAY (BYVAL pvar AS VARIANT PTR)
ParameterDescription
pvarPointer to the VARIANT.

Operator * (GetPtr)

Returns a pointer to the underlying safe array descriptor.

OPERATOR * () AS ANY PTR
Remark

You can also call the GetPtr method.


Operator LET ( = )

Assigns a DSAFEARRAY to another DSAFEARRAY.
Assigns a safe array to a DSAFEARRAY.
Assigns a variant of type VT_ARRAY to a DSAFEARRAY.

OPERATOR LET (BYREF dsa AS DSAFEARRAY)
OPERATOR LET (BYVAL psa AS SAFEARRAY PTR)
OPERATOR LET (BYVAL pvar AS VARIANT PTR)
ParameterDescription
dsaAn instance of the DSAFEARRAY class.
psaA safe array pointer.
pvarPointer to a VARIANT.

AfxStrJoin

Returns a string consisting of all of the strings in an array, each separated by a delimiter. If the delimiter is a null (zero-length) string then no separators are inserted between the string sections. If the delimiter expression is the 3-byte value of "," which may be expressed in your source code as the string literal """,""" or as Chr(34,44,34) then a leading and trailing double-quote is added to each string section. This ensures that the returned string contains standard comma-delimited quoted fields that can be easily parsed.

FUNCTION AfxStrJoin (BYREF dsa AS DSAFEARRAY, BYREF wszDelimiter AS WSTRING = " ") AS DWSTRING
ParameterDescription
dsaThe one-dimensional VT_BSTR DSAFEARRAY to join.
wszDelimiterThe delimiter character.
Return value

A DWSTRING containing the joined string.

Usage example
DIM dsa AS DSAFEARRAY = DSAFEARRAY("STRING", 3, 1)
dsa.PutStr(1, "One")
dsa.PutStr(2, "Two")
dsa.PutStr(3, "Three")
DIM dws AS DWSTRING = AfxStrJoin(dsa, ",")
PRINT dws   ' ouput: One,Two,Three

AfxStrSplit

Splits a string into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

FUNCTION AfxStrSplit (BYREF wszStr AS CONST WSTRING, BYREF wszDelimiters AS WSTRING = " ") AS DSAFEARRAY
ParameterDescription
wszStrThe string to split.
wszDelimitersThe delimiter characters.
Return value

A DSAFEARRAY containing a token in each element.

Usage example
DIM dws AS DWSTRING = "- This, a sample string."
DIM dsa AS DSAFEARRAY = AfxStrSplit(dws, " ,.-")
FOR i AS LONG = dsa.LBound TO dsa.UBound
  PRINT dsa.GetStr(i)
NEXT

AccessData

Retrieves a pointer to the array data and increments the lock count of an array.

FUNCTION AccessData () AS ANY PTR
Return value

IF it succeeds, it returns a pointer to the array data. If it fails, it returns a null pointer.


Append

Appends a value to the end of the one-dimensional safe array.

FUNCTION Append (BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION Append (BYREF bsData AS BSTRING) AS HRESULT
FUNCTION Append (BYREF dvData AS DVAEIANT) AS HRESULT
FUNCTION Append (BYVAL vData AS VARIANT) AS HRESULT

Appends a string to the end of the one-dimensional safe array.

FUNCTION AppendStr (BYVAL pwszData AS WSTRING PTR) AS HRESULT
FUNCTION AppendVar (BYREF dvData AS DVARIANT) AS HRESULT
ParameterDescription
pDataPointer to a variable of the appropriate data type.
bsDataThe BSTRING to insert, if the safe array is of type VT_BSTR.
dvDataThe DVARIANT to insert, if the safe array is of type VT_VARIANT.
vDataThe VARIANT to insert, if the safe array is of type VT_VARIANT.
Usage examples
' // Creates a one-dimensional array of doubles
'DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_R8, 2, 1)
DIM dsa AS DSAFEARRAY = DSAFEARRAY("DOUBLE", 2, 1)

DIM dblVal AS DOUBLE = 12345.12
dsa.Put(1, @dblVal)
dblVal = 74447.34
dsa.Put(2, @dblVal)
dblVal = 63535.567
dsa.Append(@dblVal)

dsa.Get(1, @dblVal)
print dblVal
dsa.Get(2, @dblVal)
print dblVal
dsa.Get(3, @dblVal)
print dblVal
' // Creates a one-dimensional array of variants
'DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_VARIANT, 2, 1)
DIM dsa AS DSAFEARRAY = DSAFEARRAY("VARIANT", 2, 1)
dsa.PutVar(1, "Test string")
dsa.PutVar(2, 12345)
dsa.AppendVar("Test string appended")

DIM dvOut AS DVARIANT
dvOut = dsa.GetVar(1)
print dvOut
dvOut = dsa.GetVar(2)
print dvOut
dvOut = dsa.GetVar(3)
print dvOut
Return value

S_OK (0) on success or an HREUSLT code on failure.

HREUSLTDescription
DISP_E_BADINDEXThe specified index is not valid.
E_INVALIDARGOne of the arguments is not valid.
E_OUTOFMEMORYMemory could not be allocated for the element.
E_FAILFailure. The array descriptor is null.

Attach

Attaches a safe array to a DSAFEARRAY object.

FUNCTION Attach (BYVAL psaSrc AS SAFEARRAY PTR) AS HRESULT
Return value

S_OK (0) on success or an HREUSLT code.


DestroyData / Clear / Erase / Reset

Destroys all the data in a safe array.

FUNCTION DestroyData () AS HRESULT
Return value

S_OK (0) on success or an HREUSLT code on failure.

HREUSLTDescription
DISP_E_ARRAYISLOCKEDThe array is locked.

Copy

Creates a copy of the safe array.

FUNCTION Copy () AS SAFEARRAY PTR
Return value

Pointer of the new array descriptor. You must free this pointer calling the API function SafeArrayDestroy.


CopyData

Copies the source array to the target array after releasing any resources in the target array. This is similar to Copy, except that the target array has to be set up by the caller. The target is not allocated or reallocated.

FUNCTION CopyData (BYVAL psaTarget AS SAFEARRAY PTR) AS HRESULT

On exit, the array referred to by psaTarget contains a copy of the data if the call succeeds.

Return value

S_OK (0) on success or an HREUSLT code.

HREUSLTDescription
E_INVALIDARGThe dimensions or the number of dimensions don't match.
E_OUTOFMEMORYInsufficient memory to create the copy.

CopyFrom

Copies the contents of the passed safe array.

FUNCTION CopyFrom (BYVAL psaSrc AS SAFEARRAY PTR) AS HRESULT
Return value

S_OK (0) on success or an HREUSLT code on failure.

Remarks

CopyFrom calls the string or variant manipulation functions if the array to copy contains either of these data types. If the array being copied contains object references, the reference counts for the objects are incremented.


CopyFromVariant

Copies the contents of a VARIANT of type VT_ARRAY to the object. The VARIANT remains unaltered.

FUNCTION CopyFromVariant (BYVAL pvar AS VARIANT PTR) AS HRESULT

pvar must be variant of type VT_ARRAY, i.e. containing a safe array. If it is of another type, an invalid data error is returned.

Return value

S_OK (0) on success or an HREUSLT code on failure.

Remarks

CopyFromVariant calls the string or variant manipulation functions if the array to copy contains either of these data types. If the array being copied contains object references, the reference counts for the objects are incremented.


CopyToVariant

Copies the safe array to the passed variant.

FUNCTION CopyToVariant (BYVAL pvar AS VARIANT PTR) AS HRESULT

pvar is a pointer to the variant where the safe array will be copied.

Return value

S_OK (0) on success or an HREUSLT code on failure.


Count

Returns the number of elements in the specified dimension of the array.

FUNCTION Count (BYVAL nDim AS UINT = 1) AS UINT

The optional nDim parameter is the array dimension for which to get the number of elements. You don't need to pass this parameter if the safe array in one-dimensional.

Return value

S_OK (0) on success or an HREUSLT code on failure.


Create

Creates a safe array from the given VARTYPE, number of dimensions and bounds.

Multidimensional array:

FUNCTION Create (BYVAL vt AS VARTYPE, BYVAL cDims AS UINT, BYVAL prgsabound AS SAFEARRAYBOUND PTR) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.
cDimsNumber of dimensions in the array. The number cannot be changed after the array is created.
rgsaboundPointer to a vector of bounds (one for each dimension) to allocate for the array.

One-dimensional array:

FUNCTION Create (BYVAL vt AS VARTYPE, BYVAL cElements AS ULONG, BYVAL lLBound AS LONG) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.
cElementsOptional. Number of elements in the array.
lLBoundOptional. The lower bound of the array.

Two-dimensional array:

FUNCTION Create (BYVAL vt AS VARTYPE, BYVAL cElements1 AS ULONG, BYVAL lLBound1 AS LONG, _
   BYVAL cElements2 AS ULONG, BYVAL lLBound2 AS LONG) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.
cElements1Number of elements in the first dimension of the array.
lLBound1The lower bound of the first dimension of the array.
cElements2Number of elements in the second dimension of the array.
lLBound2The lower bound of the second dimension of the array.
Return value

S_OK (0) on success or an HREUSLT code on failure.


CreateEx

Creates a safe array from the given VARTYPE, number of dimensions and bounds.

Multidimensional array:

FUNCTION CreateEx (BYVAL vt AS VARTYPE, BYVAL cDims AS UINT, _
   BYVAL prgsabound AS SAFEARRAYBOUND PTR, BYVAL pvExtra AS PVOID) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal. The FADF_RECORD flag can be set for a variant type VT_RECORD, The FADF_HAVEIID flag can be set for VT_DISPATCH or VT_UNKNOWN, and FADF_HAVEVARTYPE can be set for all other VARTYPEs. For more information about the FADF_RECORD, FADF_HAVEIID or FADF_HAVEVARTYPE flags see SAFEARRAY Data Type.
cDimsNumber of dimensions in the array. The number cannot be changed after the array is created.
rgsaboundPointer to a vector of bounds (one for each dimension) to allocate for the array.
pvExtraPoints to the type information of the user-defined type, if you are creating a safe array of user-defined types. If the vt parameter is VT_RECORD, then pvExtra will be a pointer to an IRecordInfo interface describing the record. If the vt parameter is VT_DISPATCH or VT_UNKNOWN, then pvExtra will contain a pointer to a GUID representing the type of interface being passed to the array.

One-dimensional array:

FUNCTION CreateEx (BYVAL vt AS VARTYPE, BYVAL cElements AS ULONG, _
   BYVAL lLBound AS LONG, BYVAL pvExtra AS PVOID) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal. The FADF_RECORD flag can be set for a variant type VT_RECORD, The FADF_HAVEIID flag can be set for VT_DISPATCH or VT_UNKNOWN, and FADF_HAVEVARTYPE can be set for all other VARTYPEs. For more information about the FADF_RECORD, FADF_HAVEIID or FADF_HAVEVARTYPE flags see SAFEARRAY Data Type.
cElementsOptional. Number of elements in the array.
lLBoundOptional. The lower bound of the array.
pvExtraPoints to the type information of the user-defined type, if you are creating a safe array of user-defined types. If the vt parameter is VT_RECORD, then pvExtra will be a pointer to an IRecordInfo interface describing the record. If the vt parameter is VT_DISPATCH or VT_UNKNOWN, then pvExtra will contain a pointer to a GUID representing the type of interface being passed to the array.

Two-dimensional array:

FUNCTION CreateEx (BYVAL vt AS VARTYPE, BYVAL cElements1 AS ULONG, BYVAL lLBound1 AS LONG, _
   BYVAL cElements2 AS ULONG, BYVAL lLBound2 AS LONG, BYVAL pvExtra AS PVOID) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal. The FADF_RECORD flag can be set for a variant type VT_RECORD, The FADF_HAVEIID flag can be set for VT_DISPATCH or VT_UNKNOWN, and FADF_HAVEVARTYPE can be set for all other VARTYPEs. For more information about the FADF_RECORD, FADF_HAVEIID or FADF_HAVEVARTYPE flags see SAFEARRAY Data Type.
cElements1Number of elements in the first dimension of the array.
lLBound1The lower bound of the first dimension of the array.
cElements2Number of elements in the second dimension of the array.
lLBound2The lower bound of the second dimension of the array.
pvExtraPoints to the type information of the user-defined type, if you are creating a safe array of user-defined types. If the vt parameter is VT_RECORD, then pvExtra will be a pointer to an IRecordInfo interface describing the record. If the vt parameter is VT_DISPATCH or VT_UNKNOWN, then pvExtra will contain a pointer to a GUID representing the type of interface being passed to the array.
Return value

S_OK (0) on success or an HRESULT code on failure.


CreateVector

Creates a fixed size safe array from the given VARTYPE, lower bound and number of elements.

FUNCTION CreateVector (BYVAL vt AS VARTYPE, BYVAL cElements AS ULONG, BYVAL lLBound AS LONG) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal.
cElementsThe number of elements in the array.
lLBoundThe lower bound for the array. Can be negative.
Return value

S_OK (0) on success or an HRESULT code on failure.

Remarks

CreateVector allocates a single block of memory containing a SAFEARRAY structure for a single-dimension array (24 bytes), immediately followed by the array data. All of the existing safe array functions work correctly for safe arrays that are allocated with CreateVector.

A safe array created with CreateVector is allocated as a single block of memory. Both the SafeArray descriptor and the array data block are allocated contiguously in one allocation, which speeds up array allocation. However, a user can allocate the descriptor and data area separately using the SafeArrayAllocDescriptor and SafeArrayAllocData* calls.

Usage example
DIM dsa AS DSAFEARRAY
dsa.CreateVector(VT_VARIANT, 1000, 1)
FOR i AS LONG = 1 TO 1000
   dsa.PutVar(i, "Test string " & WSTR(i))
NEXT

CreateVectorEx

Creates a fixed size safe array from the given VARTYPE, lower bound and number of elements.

FUNCTION CreateVectorEx (BYVAL vt AS VARTYPE, BYVAL cElements AS ULONG, BYVAL lLBound AS LONG, _
   BYVAL pvExtra AS ANY PTR) AS HRESULT
ParameterDescription
vtThe base type of the array (the VARTYPE of each element of the array). The VARTYPE is restricted to a subset of the variant types. Neither the VT_ARRAY nor the VT_BYREF flag can be set. VT_EMPTY and VT_NULL are not valid base types for the array. All other types are legal. The FADF_RECORD flag can be set for a variant type VT_RECORD, The FADF_HAVEIID flag can be set for VT_DISPATCH or VT_UNKNOWN, and FADF_HAVEVARTYPE can be set for all other VARTYPEs. For more information about the FADF_RECORD, FADF_HAVEIID or FADF_HAVEVARTYPE flags see SAFEARRAY Data Type.
cElementsThe number of elements in the array.
lLBoundThe lower bound for the array. Can be negative.
pvExtraPoints to the type information of the user-defined type, if you are creating a safe array of user-defined types. If the vt parameter is VT_RECORD, then pvExtra will be a pointer to an IRecordInfo interface describing the record. If the vt parameter is VT_DISPATCH or VT_UNKNOWN, then pvExtra will contain a pointer to a GUID representing the type of interface being passed to the array.
Return value

S_OK (0) on success or an HRESULT code on failure.


Destroy

Destroys an existing array descriptor and all of the data in the array. If objects are stored in the array, Release is called on each object in the array.

FUNCTION Destroy () AS HRESULT
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
E_INVALIDARGThe dimensions or the number of dimensions don't match.
DISP_E_ARRAYISLOCKEDThe array is currently locked.

Detach

Detaches the safe array descriptor from the DSAFEARRAY.

FUNCTION Detach () AS SAFEARRAY PTR
Return value

Returns a pointer to a SAFEARRAY descriptor.

Remarks

The caller takes ownership of it and must destroy it when no longer needed.


ElemSize

Returns the size of an element.

FUNCTION ElemSize () AS UINT
Return value

Returns the size (in bytes) of an element in a safe array. Does not include size of pointed-to data.


Features / Flags

Returns the flags used by the safe array. This is the same that the Flags method.

FUNCTION Features () AS USHORT

Find

Scans the array to search for the specified string.

FUNCTION Find (BYREF wszFind AS WSTRING, BYVAL IgnoreCase AS BOOLEAN = TRUE) AS LONG
ParameterDescription
wszFindThe string to find.
IgnoreCaseOptional. TRUE = Ignore case.
Return value

The index of the retrieved array element, or 0 on failure.

Usage example
' // Create a one-dimensional array of strings
DIM dsa AS DSAFEARRAY = DSAFEARRAY("BSTR", 2, 1)
dsa.PutStr(1, "Test string 1")
dsa.PutStr(2, "Test string 2")
dsa.InsertStr(2, "12345.67")
DIM nPos AS LONG = dsa.Find("Test string 2")
PRINT nPos

Get / GetStr / GetVar

Retrieves a single element of the array.

Multidimensional array:

FUNCTION Get (BYVAL prgIndices AS LONG PTR, BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION Get (BYVAL prgIndices AS LONG PTR, BYREF bsData AS BSTRING) AS HRESULT
FUNCTION Get (BYVAL prgIndices AS LONG PTR, BYREF dvData AS DVARIANT) AS HRESULT
FUNCTION GetStr (BYVAL prgIndices AS LONG PTR) AS BSTRING
FUNCTION GetVar (BYVAL prgIndices AS LONG PTR) AS DVARIANT
ParameterDescription
prgIndicesPointer to a vector of indexes for each dimension of the array. The right-most (least significant) dimension is rgIndices(0). The left-most dimension is stored at pgIndices(@psa.cDims – 1).
pDataPointer to the location to place the element of the array.
bsDataA BSTRING passed by reference that will receive the result.
dvDataA DVARIANT passed by reference that will receive the result.

One-dimensional array:

FUNCTION Get (BYVAL idx AS LONG, BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION Get (BYVAL idx AS LONG, BYREF bsData AS BSTRING) AS HRESULT
FUNCTION Get (BYVAL idx AS LONG, BYREF dvData AS DVARIANT) AS HRESULT
FUNCTION GetStr (BYVAL idx AS LONG) AS BSTRING
FUNCTION GetVar (BYVAL idx AS LONG) AS DVARIANT
ParameterDescription
idxIndex of the element.
pDataPointer to the location to place the element of the array.
bsDataA BSTRING passed by reference that will receive the result.
dvDataA DVARIANT passed by reference that will receive the result.

Two-dimensional array:

FUNCTION Get (BYVAL cElem AS LONG, BYVAL cDim AS LONG, BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION Get (BYVAL cElem AS LONG, BYVAL cDim AS LONG, BYREF bsData AS BSTRING) AS HRESULT
FUNCTION Get (BYVAL cElem AS LONG, BYVAL cDim AS LONG, BYREF dvData AS DVARIANT) AS HRESULT
FUNCTION GetStr (BYVAL cElem AS LONG, BYVAL cDim AS LONG) AS BSTRING
FUNCTION GetVar (BYVAL cElem AS LONG, BYVAL cDim AS LONG) AS DVARIANT
ParameterDescription
cElemIndex of the element.
cDimDimension of the array.
pDataPointer to the location where to place the element of the array.
bsDataA BSTRING passed by reference that will receive the result.
dvDataA DVARIANT passed by reference that will receive the result.

First element, then dimension, e.g. 2, 1 (element 2, first dimension), 1, 2 (element 1, 2nd dimension).

Usage example
' // One-dimensional array of VT_BSTR
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_BSTR, 2, 1)
dsa.PutStr(1, "Test string 1")
print dsa.GetStr(1)
dsa.PutStr(2, "Test string 2")
print dsa.GetStr(2)
' // One-dimensional array of VT_VARIANT
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_VARIANT, 5, 1)
DIM dv1 AS DVARIANT = "Test variant 1"
dsa.Put(1, dv1)
DIM dvOut AS DVARIANT
dsa.Get(1, dvOut)
print dvOut
DIM dv2 AS DVARIANT = "Test variant 2"
dsa.Put(1, dv2)
dsa.Get(1, dvOut)
print dvOut
' // Two-dimensional array of BSTR
' // 2D: elements = 5, lower bound = 1
' // 2D: elements = 3, lower bound = 1
DIM rgsabounds(0 TO 1) AS SAFEARRAYBOUND = {(5, 1), (3, 1)}
DIM csa AS DSAFEARRAY = DSAFEARRAY(VT_BSTR, 2, @rgsabounds(0))

' // array index: first element, first dimension
DIM rgidx(0 TO 1) AS LONG = {1, 1}
DIM bs1 AS BSTRING = "Test string 1"
' // Put the value
dsa.Put(@rgidx(0), bs1)
' // Get the value
DIM bsOut AS BSTRING
dsa.Get(@rgidx(0), bsOut)
print bsOut
' // array index: second element, first dimension
rgidx(0) = 2 : rgidx(1) = 1
' // Put the value
DIM bs2 AS BSTRING = "Test string 2"
dsa.Put(@rgidx(0), bs2)
' // Get the value
dsa.Get(@rgidx(0), bsOut)
print bsOut

' // array index: first element, second dimension
rgidx(0) = 1 : rgidx(1) = 2
' // Put the value
DIM bs3 AS BSTRING = "Test string 3"
dsa.Put(@rgidx(0), bs3)
' // Get the value
dsa.Get(@rgidx(0), bsOut)
print bsOut

' // array index: second element, second dimension
rgidx(0) = 2 : rgidx(1) = 2
' // Put the value
DIM bs4 AS BSTRING = "Test string 4"
dsa.Put(@rgidx(0), bs4)
' // Get the value
dsa.Get(@rgidx(0), bsOut)
print bsOut
Return value (Get)

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
DISP_E_BADINDEXThe specified index is invalid.
E_INVALIDARGOne of the arguments is invalid.
E_OUTOFMEMORYMemory could not be allocated for the element.
Return value (GetStr / GetVar)

The string or variant element.

Remarks

This method calls SafearrayLock and SafearrayUnlock automatically, before and after retrieving the element. The caller must provide a storage area of the correct size to receive the data. If the data element is a string, object, or variant, the function copies the element in the correct way.


GetIID

Returns the GUID of the interface contained within a given safe array.

FUNCTION GetIID () AS GUID
Return value

The GUID of the interface, on success, or a null guid on failure.


GetRecordInfo

Retrieves the IRecordInfo interface of a UDT contained in a given safe array.

FUNCTION GetRecordInfo () AS IRecordInfo PTR

GetType

Returns the VARTYPE stored in the given safe array.

FUNCTION GetType () AS VARTYPE

Insert

Inserts a value at the specified position of the safe array.

FUNCTION Insert (BYVAL nPos AS LONG, BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION Insert (BYVAL nPos AS LONG, BYREF bsData AS BSTRING) AS HRESULT
FUNCTION Insert (BYVAL nPos AS LONG, BYREF dvData AS DVARIANT) AS HRESULT
FUNCTION Insert (BYVAL nPos AS LONG, BYVAL vData AS VARIANT) AS HRESULT

Inserts a value at the beginning of the safe array.

FUNCTION Insert (BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION Insert (BYREF bsData AS BSTRING) AS HRESULT
FUNCTION Insert (BYREF dvData AS DVARIANT) AS HRESULT
FUNCTION Insert (BYVAL vData AS VARIANT) AS HRESULT

Inserts a string.

FUNCTION InsertStr (BYVAL nPos AS LONG, BYVAL pwszData AS WSTRING PTR) AS HRESULT
FUNCTION InsertStr (BYVAL pwszData AS WSTRING PTR) AS HRESULT

Inserts a variant.

FUNCTION InsertVar (BYVAL nPos AS LONG, BYREF dvData AS DVARIANT) AS HRESULT
FUNCTION InsertVar (BYREF dvData AS DVARIANT) AS HRESULT
ParameterDescription
nPosOptional. Index of the array in which the data will be inserted. If nPos is not specified, the item is inserted at the beginning of the array. If the array is empty, it is redimensioned to one element.
pDataPointer to a variable of the appropriate data type.
bsDataThe BSTRING to insert, if the safe array is of type VT_BSTR.
dvDataThe DVARIANT to insert, if the safe array is of type VT_VARIANT.
vDataThe VARIANT to insert, if the safe array is of type VT_VARIANT.
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
DISP_E_BADINDEXThe specified index is invalid.
E_INVALIDARGOne of the arguments is invalid.
E_OUTOFMEMORYMemory could not be allocated for the element.
E_FAILFailure. The array descriptor is null.
Usage example
' // Create a one-dimensional array of variants
'DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_VARIANT, 2, 1)
DIM dsa AS DSAFEARRAY = DSAFEARRAY("VARIANT", 2, 1)

dsa.PutVar(1, "Test string 1")
dsa.PutVar(2, "Test string 2")
dsa.InsertVar(2, 12345.67)

DIM dvOut AS DVARIANT
dsa.Get(1, dvOut)
print dvOut
dsa.Get(2, dvOut)
print dvOut
dsa.Get(3, dvOut)
print dvOut

IsResizable

Tests if the safe array can be resized.

FUNCTION IsResizable () AS BOOLEAN
Return value

TRUE if the array can be resized; FALSE, otherwise.


LBound

Returns the lower bound for any dimension of a safe array.

FUNCTION LBound (BYVAL nDim AS UINT = 1) AS LONG
ParameterDescription
nDimOptional. The array dimension for which to get the lower bound. You don't need to pass this parameter if it is a one-dimensional array.

UBound

Returns the upper bound for any dimension of a safe array.

FUNCTION UBound (BYVAL nDim AS UINT = 1) AS LONG
ParameterDescription
nDimOptional. The array dimension for which to get the upper bound. You don't need to pass this parameter if it is a one-dimensional array.

LocksCount

Returns the number of times the array has been locked without the corresponding unlock.

FUNCTION LocksCount () AS UINT

MoveFromVariant

Transfers ownership of the safe array contained in the variant parameter to this object. The variant is then changed to VT_EMPTY.

FUNCTION MoveFromVariant (BYVAL pvar AS VARIANT PTR) AS HRESULT
ParameterDescription
pvarPointer to the variant containing the safe array.
Return value

S_OK (0) on success or an HRESULT code on failure.


MoveToVariant

Transfers ownership of the safe array to a variant and detaches it from the class.

FUNCTION MoveToVariant (BYVAL pvar AS VARIANT PTR) AS HRESULT
ParameterDescription
pvarPointer to the variant where the safe array will be moved.
Usage example
' // One-dimensional array of VT_BSTR
DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_BSTR, 2, 1)
dsa.PutStr(1, BSTRING("Test string 1"))
DIM bsOut AS BSTRING
bsout = dsa.GetStr(1)
print bsOut
dsa.PutStr(2, BSTRING("Test string 2"))
bsOut = dsa.GetStr(2)
print bsOut

DIM v AS VARIANT
dsa.MoveToVariant(@v)
print AfxVarToStr(@v)
VariantClear @v
Return value

S_OK (0) on success or an HRESULT code on failure.


NumDims

Returns the number of dimensions in the array.

FUNCTION NumDims () AS UINT

PtrOfIndex

Returns a pointer to an array element.

Multidimensional array:

FUNCTION PtrOfIndex (BYVAL prgIndices AS LONG PTR) AS ANY PTR
ParameterDescription
prgIndicesAn array of index values that identify an element of the array. All indexes for the element must be specified.

One-dimensional array:

FUNCTION PtrOfIndex (BYVAL idx AS LONG) AS HRESULT
ParameterDescription
idxIndex of the element.

Two-dimensional array:

FUNCTION PtrOfIndex (BYVAL cElem AS LONG, BYVAL cDim AS LONG) AS HRESULT
ParameterDescription
cElemIndex of the element.
cDimDimension number of the array.

Put

Stores the data element at a given location in the array.

Multidimensional array:

FUNCTION Put (BYVAL prgIndices AS LONG PTR, BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION PutStr (BYVAL prgIndices AS LONG PTR, BYREF bsData AS BSTRING) AS HRESULT
FUNCTION PutVar (BYVAL prgIndices AS LONG PTR, BYREF dvData AS DVARIANT) AS HRESULT
ParameterDescription
prgIndicesPointer to a vector of indexes for each dimension of the array. The right-most (least significant) dimension is rgIndices(0). The left-most dimension is stored at prgIndices(@psa.cDims – 1).
pDataPointer to the data to assign to the array. The variant types VT_DISPATCH, VT_UNKNOWN, and VT_BSTR are pointers, and do not require another level of indirection.
bsDataA BSTRING.
bsDataA DVARIANT.

One-dimensional array:

FUNCTION Put (BYVAL idx AS LONG, BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION PutStr (BYVAL idx AS LONG, BYREF bsData AS BSTRING) AS HRESULT
FUNCTION PutVar (BYVAL idx AS LONG, BYREF dvData AS DVARIANT) AS HRESULT
ParameterDescription
idxIndex of the element of the array.
pDataPointer to the data to assign to the array. The variant types VT_DISPATCH, VT_UNKNOWN, and VT_BSTR are pointers, and do not require another level of indirection.
bsDataA BSTRING.
bsDataA DVARIANT.
ParameterDescription
idxIndex of the element.

Two-dimensional array:

FUNCTION Put (BYVAL cElem AS LONG, BYVAL cDim AS LONG, BYVAL pData AS ANY PTR) AS HRESULT
FUNCTION PutStr (BYVAL cElem AS LONG, BYVAL cDim AS LONG, BYREF bsData AS BSTRING) AS HRESULT
FUNCTION PutVar (BYVAL cElem AS LONG, BYVAL cDim AS LONG, BYREF dvData AS DVARIANT) AS HRESULT
ParameterDescription
cElemIndex of the element of the array.
cDimDimension number of the array.
pDataPointer to the data to assign to the array. The variant types VT_DISPATCH, VT_UNKNOWN, and VT_BSTR are pointers, and do not require another level of indirection.
bsDataA BSTRING.
bsDataA DVARIANT.
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
DISP_E_BADINDEXThe specified index is invalid.
E_INVALIDARGOne of the arguments is invalid.
E_OUTOFMEMORYMemory could not be allocated for the element.
E_FAILFailure. The array descriptor is null.
Remarks

This function automatically calls SAfeArrayLock and SafeArrayUnlock before and after assigning the element. If the data element is a string, object, or variant, the function copies it correctly when the safe array is destroyed. If the existing element is a string, object, or variant, it is cleared correctly. If the data element is a VT_DISPATCH or VT_UNKNOWN, AddRef is called to increment the object's reference count.

Multiple locks can be on an array. Elements can be put into an array while the array is locked by other operations.


Redim

Changes the right-most (least significant) bound of a safe array.

Multidimensional array:

FUNCTION Redim (BYVAL pnewsabounds AS SAFEARRAYBOUND PTR) AS HRESULT
ParameterDescription
pnewsaboundsPointer to a new safe array bound structure that contains the new array boundary. You can change only the least significant dimension of an array.

One-dimensional array:

FUNCTION Redim (BYVAL cElements AS ULONG, BYVAL lLBound AS LONG) AS HRESULT
ParameterDescription
cElementsNumber of elements in the array.
lLBoundThe lower bound of the array.

Two-dimensional array:

FUNCTION Redim (BYVAL cElements1 AS ULONG, BYVAL lLBound1 AS LONG, _
   BYVAL cElements2 AS ULONG, BYVAL lLBound2 AS LONG) AS HRESULT
ParameterDescription
cElements1Number of elements in the first dimension of the array
lLBound1The lower bound of the first dimension of the array.
cElements2Number of elements in the second dimension of the array
lLBound2The lower bound of the second dimension of the array.
Usage example
' // One-dimensional array of VT_VARIANT
' // Two elements, lower-bound 1
'DIM dsa AS DSAFEARRAY = DSAFEARRAY(VT_VARIANT, 2, 1)
DIM dsa AS DSAFEARRAY = DSAFEARRAY("VARIANT", 2, 1)
DIM dv1 AS DVARIANT = "Test variant 1"
dsa.PutVar(1, dv1)
DIM dvOut AS DVARIANT
dvOut = dsa.GetVar(1)
print dvOut

DIM dv2 AS DVARIANT = "Test variant 2"
dsa.PutVar(1, dv2)
dvOut = dsa.GetVar(1)
print dvOut

' // Redim (preserve) the safe array
dsa.Redim(1, 3)
DIM dv3 AS DVARIANT = "Test variant 3"
dsa.PutVar(3, dv3)
dvOut = dsa.GetVar(3)
print dvOut
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
DISP_E_ARRAYISLOCKEDThe array is currently locked.
E_INVALIDARGInvalid safe array descriptor.
E_FAILFailure. The array descriptor is null.

Remove

Removes the specified array element.

FUNCTION Remove (BYVAL nPos AS LONG) AS HRESULT
FUNCTION RemoveStr (BYVAL nPos AS LONG) AS HRESULT
FUNCTION RemoveVar (BYVAL nPos AS LONG) AS HRESULT
ParameterDescription
nPosIndex of the array element which will be deleted.
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
DISP_E_BADINDEXThe specified index is not valid.
E_INVALIDARGOne of the arguments is not valid.
E_OUTOFMEMORYMemory could not be allocated for the element.
E_FAILFailure. The array descriptor is null.

SetIID

Sets the IID (a GUID) of the interface contained within a given safe array.

FUNCTION SetIID (BYVAL pguid AS GUID PTR) AS HRESULT
ParameterDescription
pguidPointer to the IID.
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
E_INVALIDARGIf the array descriptor does not have the FADF_HAVEIID flag set.
E_FAILFailure. The array descriptor is null.

SetRecordInfo

Sets the IRecordInfo interface of the UDT contained in a given safe array.

FUNCTION SetRecordInfo (BYVAL prinfo AS IRecordInfo PTR) AS HRESULT
ParameterDescription
prinfoPointer to an IRecordInfo interface.
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
E_INVALIDARGIf the array descriptor does not have the FADF_RECORD flag set.
E_FAILFailure. The array descriptor is null.

Sort

Sorts a one-dimensional VT_BSTR DSAFEARRAY calling the C qsort function.

FUNCTION Sort (BYVAL bAscend AS BOOLEAN = TRUE) AS HRESULT
ParameterDescription
bAscendTRUE for sorting in ascending order; FALSE for sorting in descending order.
Return value

S_OK (0) on success or an HRESULT code on failure.

HRESULTDescription
E_FAILThe array descriptor is null or the safe array is not of of the type VT_BSTR.
E_UNEXPECTEDThe array could not be locked.
Usage example
' // Create a one-dimensional array of BSTR
DIM bsVal AS BSTRING = "bcde"
dsa.PutStr(1, bsVal)
bsVal = "abc"
dsa.PutStr(2, bsVal)
bsVal = "abcfg"
dsa.PutStr(3, bsVal)
' // Sort the safe array
dsa.Sort

bsVal = dsa.GetStr(1)
print bsVal
bsVal = dsa.GetStr(2)
print bsVal
bsVal = dsa.GetStr(3)
print bsVal

UnaccessData

Decrements the lock count of an array, and invalidates the pointer retrieved by AccessData.

FUNCTION UnaccessData () AS HRESULT
Return value

S_OK (0) on success or an HRESULT code on failure.