In the com method, array data can be transmitted using the variant type and processed using safearray in VC ++. The following example shows the input and output arrays.
1. Input an array to com
Stdmethodimp ctestcom1: vb2vc (variant buffer)
{
Long dim = safearraygetdim (buffer. parray );
Long ubound;
Long lbound;
Safearraygetubound (buffer. parray, dim, & ubound );
Safearraygetlbound (buffer. parray, dim, & lbound );
BSTR * Buf;
BSTR Pd [2];
Safearrayaccessdata (buffer. parray, (void **) & BUF );
For (INT I = lbound; I <ubound; I ++)
Pd [I] = Buf [I];
}
Return s_ OK;
}
Buffer is a one-dimensional array that stores strings. The code in VB is
Dim oo as arraytestlib. testcom1
Set oo = new arraytestlib. testcom1
Dim Buf (2) as string
Buf (0) = "65"
Buf (1) = "anss"
OO. vb2vc Buf
2. Com returns array data to VB
Stdmethodimp ctestcom1: retarray (variant * buffer)
{
// Returns an array.
Safearray far * PSA;
Safearraybound rgsabound [1];
Rgsabound [0]. llbound = 0;
Rgsabound [0]. celements = 2;
PSA = safearraycreate (vt_i4, 1, rgsabound );
Long idx;
Long setdt;
Idx = 0;
Setdt = 12;
Safearrayputelement (PSA, & idx, & setdt );
Idx = 1;
Set setdt = 342;
Safearrayputelement (PSA, & idx, & setdt );
V_vt (buffer) = vt_array | vt_i4;
V_array (buffer) = psa;
Return s_ OK;
}
The code in VB is:
Dim oo as arraytestlib. testcom1
Set oo = new arraytestlib. testcom1
Dim rarr as Variant
OO. retarray rarr
Msgbox rarr (0) & rarr (1)