COM to get the JavaScript array size

Source: Internet
Author: User
Tags javascript array

When the Helper House (www.bkjia.com) Tutorial uses JavaScript to call COM to pass arrays, the COM interface receives VARIANT objects, such as array, the vt type VT_DISPATCH of the object, therefore, the value indicates an IDispatch pointer.

The pointer of the IDispatch type indicates that the array object is actually a JavaScript built-in array object. On the JavaScript side, we can get the size of the array through the length attribute, you can use the GetIDsOfNames and Invoke functions to obtain the array length. In this way, you can dynamically variable the array content.

Copy to ClipboardReference content: [www.bkjia.com] // obtain the array Length
BSTR bstrLength = L "length ";
DISPID dispid;
Hr = lpDispatch-> GetIDsOfNames (IID_NULL, & bstrLength, 1,
LOCALE_USER_DEFAULT, & dispid );
If (SUCCEEDED (hr ))
{
CComVariant varResult;
Hr = lpDispatch-> Invoke (dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT,
DISPATCH_PROPERTYGET, & noArgs, & varResult, NULL, NULL );


If (varResult. vt = VT_I4)
{
NLength = varResult. intVal;
}
}

In this case, nLength obtains the length of the array.

When an array in JavaScript is an object, the array content is the property of the object and is dynamically created. The query method of these attributes is similar to that of length, it is also a GetIDsOfNames and Invoke function. The main difference lies in the difference in the name. The attribute of the element object in the array. Its name is dynamically created, that is, it can be obtained through subscript. Therefore, here, you can also obtain the attribute name by subscript, as shown in the following code:

Copy to ClipboardReference: [www.bkjia.com] for (int I = 0; I <nLength; ++ I)
{
CComVariant vaIndex (I, VT_I4 );
VaIndex. ChangeType (VT_BSTR );
DISPID dispid;
Hr = lpDispatch-> GetIDsOfNames (IID_NULL, & vaIndex. bstrVal, 1,
LOCALE_USER_DEFAULT, & dispid );

If (FAILED (hr ))
{
Continue;
}

CComVariant varResult;
Hr = lpDispatch-> Invoke (dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT,
DISPATCH_PROPERTYGET, & noArgs, & varResult, NULL, NULL );

VARTYPE vt = varResult. vt;
If (vt = VT_DISPATCH)
{
InvokeArray (varResult );
Continue;
}

Hr = varResult. ChangeType (VT_BSTR );
CComBSTR bstrVal = varResult. bstrVal;

}

Therefore, by calling these two attribute methods, you can facilitate all JavaScript array objects in the COM interface.

What are the advantages of this? When looking at a lot of online resources, we found that most of the structures are processed using the SAFEARRAY method and passed into the COM interface. However, SAFEARRAY is not supported in MIDL, the JavaScript Object itself does not support this content. to operate on the SAFEARRAY method, you need to switch the VBScript and JavaScript languages, which leads to difficulties in programming and the confusion of maintenance personnel.

Directly pass in the array in JavaScript mode and integrate the array in any way. You do not need to integrate the struct in SAFEARRAY mode. At the same time, because every object (element) in JavaScript carries its own type information, the optimal replacement method (Transfer Method) of the struct in C when arrays in JavaScript are used ).

Http://www.cnblogs.com/ubunoon/

Related Article

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.