Use of safearray

Source: Internet
Author: User

To pass arrays in COM, standard C ++ Arrays can be directly used between C ++ and even VJ ++. To pass arrays to VB, use safearray.
Safearray is more powerful than ordinary simple arrays, but it is more complicated to use. Win32 provides a set of APIs to operate safearray,
However, these APIs are not easy to use, so VC and CB both have specialized classes that encapsulate safearray operations.

The tsafearray class is provided in CB, which is a template class. Many parameters need to be passed to generate a template class instance. it is inconvenient to use, but it is okay,
Typedef is provided for several common arrays in CB. For example, to define a two-dimensional array of BSTR, you can use tsafearraybstr2,
To define a three-dimensional long array, use tsafearraylong3. for example, we need to define a first-dimensional 3 element,
The two-dimensional BSTR array of the fourth element can be written as follows: tsafearraydim2 dim (3, 4); tsafearraybstr2
Bstrarray (DIM); this defines a two-dimensional BSTR array named bstrarray. For access to tsafearray, you can directly use [],
Because the tsafearray class overload the [] Operator. For example, to access the third element in the second dimension of the preceding array, directly use bstrarray [1] [2.
An array of the BSTR type must be separately described. BSTR returned by the access method bstrarray [1] [2] is not a reference,
Instead, a new BSTR copy is created and then returned. Therefore, when using this type of object, you must manually delete it after the BSTR is returned.
Otherwise, memory leakage occurs.
The last point to note about tsafearray is that the tsafearray class implementation in cb5 has a bug,
We need to manually modify row 223rd of $ BCB/include/VCL/safearry. h file. Delete []
M_indices; changed to {m_indices-; Delete [] m_indices ;}

The colesafearray class is provided in VC to encapsulate safearray. colesafearray without overloading [].
Therefore, you need to use getelement and setelement to set values. And colesafearray is not a template class, so it is not of type security. In addition,
Note that BSTR is still used. The BSTR obtained from getelement is a copy of the original safearray and needs to be manually released after use. In addition,
If a safearray object packaged with variant is passed from the COM interface,
Generally, we can use this variant to construct the colesafearray. However, after use, we also need to use variantclear to release the variant.
The following example uses putelement to construct a colesafearray and getelement to create a colesafearray
Retrieve the elements in the array, and use variantclear to clear the array:

/* Construct a colesafearray Array Using putelement */
Variant val;
Colesafearray Saret;
DWORD numelements [] = {10, 10}; // 10x10
// Create the 2 dimen1_safe-array of Type vt_bstr with size 10x10
Saret. Create (vt_bstr, 2, numelements );
// Initialize safearray with values...
Long index [2];
For (index [0] = 0; index [0] <10; index [0] ++)
{
For (index [1] = 0; index [1] <10; index [1] ++)
{
Cstring STR ("donkey ");
BSTR = Str. allocsysstring ();
// Populate the safearray elements with BSTR values
Saret. putelement (index, BSTR );
// Free BSTR
: Sysfreestring (BSTR );
}
}

// Return the safe-array encapsulated in a variant...
Val = Saret. Detach ();
Val. Vt = vt_array | vt_bstr;

/* Use getelement to retrieve the elements in the colesafearray */
// Determine upper bounds for both dimensions
Long lnumrows;
Long lnumcols;
Saret. getubound (1, & lnumrows );
Saret. getubound (2, & lnumcols );
// Display the elements in the safearray.
Colesafearray safearray (& Val );
DWORD dim = safearray. getdim ();
// Determine upper bounds for both dimensions
Long R, C;
Saret. getlbound (1, & R );
Saret. getlbound (2, & C );
For (; r <= lnumrows; r ++)
{
For (; C <= lnumcols; C ++)
{
Index [0] = R;
Index [1] = C;
BSTR;
// Retrieve each element of the safearray
Saret. getelement (index, & BSTR );
Int nelemlen =: sysstringbytelen (BSTR );
Char strtemp [20];
Memcpy (strtemp, (char *) BSTR, nelemlen );
: Sysfreestring (BSTR );
}
}

Variantclear (& Val );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.