Use the variant type to pass array data in the com Method

Source: Internet
Author: User
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)

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.