Home > Web Front-end > JS Tutorial > body text

Code to get JavaScript array size in COM_javascript skills

WBOY
Release: 2016-05-16 18:41:36
Original
1365 people have browsed it

A pointer of type IDispatch indicates that the array object is actually a built-in array object of JavaScript. On the JavaScript side, we can get the size of the array through the length attribute. Then here, we can obtain the array through the GetIDsOfNames function and the Invoke function. length, so that the contents of the array can be dynamically variable.

Copy code The code is as follows:

// Get 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;
}
}

At this time, nLength gets the length of the array.

An array in JavaScript is an object, and the contents of the array are the attributes of the object, which are dynamically created. The query method of these attributes is somewhat similar to the query method of length. They are also GetIDsOfNames and Invoke functions. The main functions are The difference lies in the name. The name of the attribute of the element object in the array is dynamically created, that is, it can be obtained through subscripting. Therefore, here, the attribute name can also be obtained through subscripting, as follows:

Copy code The code is as follows:

for ( int i=0; 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, all JavaScript array objects can be facilitated in the COM interface.

What are the benefits of this? When looking at many online resources, I found that most of them use SAFEARRAY to process structures and pass them into the COM interface, but SAFEARRAY is not supported in MIDL, and The JavaScript object itself does not support this content. To operate in SAFEARRAY mode, you need to switch between VBScript and JavaScript. This will cause difficulties in program writing and confusion for maintenance personnel.

Directly use JavaScript to pass in the array, and integrate the array in any way. There is no need to integrate the structure through SAFEARRAY. At the same time, since every object (element) in JavaScript has its own type information, arrays in JavaScript are the best alternative (transfer method) to structures in C.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template