When JavaScript calls COM to pass an array, the COM interface receives a VARIANT type object, such as array. The vt type of this object is VT_DISPATCH, so its value represents an IDispatch type pointer.
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.
The following is the quoted content: // Get the length of the array } |
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 difference is the difference in names. 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:
The following is the quoted content: for ( int i = 0 ; i < nLength; ++ i) |
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 the structure and pass it into the COM interface. However, SAFEARRAY is not supported in MIDL, and the JavaScript object itself is not supported. For this content, to operate in SAFEARRAY mode, you need to switch between VBScript and JavaScript, which will cause difficulty 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 the SAFEARRAY method. 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.