When JavaScript calls com to pass an array, the COM interface receives a variant-type object, such as array. The VT-type vt_dispatch of this 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.
// Get array Length
BSTR bstrlength = L " Length " ;
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:
For ( Int I = 0 ; I < Nlength; ++ I)
{
Ccomvariant vaindex (I, vt_i4 );
Vaindex. changetype (vt_bstr );
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 causesProgramDifficulties in writing and confusions 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 ).
Note:
Today at Google, we found that someone has used this article on a commercial website.Article, But I didn't give the source. It was just a few surprises!
Source:Http://www.cnblogs.com/ubunoon/archive/2009/11/20/COM_JAVASCRIPT.html
This article can be reproduced, but the source must be identified! We hereby declare that the copyright will be retained!