Javascript retrieves the array returned by COM or ATL (an integer or string array)
Recently, I encountered a problem in my work, that is, the string array returned by ATL could not be obtained in the js script. So I went online to see if there was any solution, but the overall feeling was messy, of course, we also see an important piece of information,
Thanks to the author, I found that this was also the case for a post posted in article 05. No one answered the question. I was lucky enough to solve this problem. I hope you can give me some comments.
First, return the string array as an example:
The implementation in ATL is as follows:
Idl definition: [id (368), helpstring ("method TestStringArr")] HRESULT TestStringArr ([out, retval] VARIANT * pCode );
. H definition: STDMETHOD (TestStringArr) (VARIANT * pCode );
. Cpp implementation:
STDMETHODIMP CNsoControl: TestStringArr (VARIANT * pCode)
{
SAFEARRAY * psa;
SAFEARRAYBOUND rgsabound [1];
Rgsabound [0]. cElements = 3;
Rgsabound [0]. lLbound = 0;
Psa = SafeArrayCreate (VT_VARIANT, 1, rgsabound); // I didn't use VT_VARIANT here, but used VT_BSTR. The result will cause new VBArray errors in JS Code.
Long idx;
VARIANT setdt;
Setdt. vt = VT_BSTR;
// Assign a value
Idx = 0;
Setdt. bstrVal = L "";
SafeArrayPutElement (psa, & idx, & setdt );
Idx = 1;
Setdt. bstrVal = L "B ";
SafeArrayPutElement (psa, & idx, & setdt );
// Assign a value
Idx = 2;
Setdt. bstrVal = L "c ";
SafeArrayPutElement (psa, & idx, & setdt );
(* PCode). vt = VT_ARRAY | VT_VARIANT; // I didn't use VT_VARIANT here, but used the VT_BSTR type. The result will cause new VBArray errors in JS Code. Please note that
(* PCode). parray = psa;
Return S_ OK;
}
Likewise, an integer array is returned as follows:
STDMETHODIMP CNsoControl: TestStringArr (VARIANT * pCode)
{
Safearray far * psa;
// Array dimension
SAFEARRAYBOUND rgsabound [1];
Rgsabound [0]. lLbound = 0;
Rgsabound [0]. cElements = 2;
Psa = SafeArrayCreate (VT_VARIANT, 1, rgsabound); // I didn't use VT_VARIANT here, but used VT_I4. The results will cause new VBArray errors in JS Code.
Long idx;
VARIANT setdt;
Setdt. vt = VT_I4;
// Assign a value
Idx = 0;
Setdt. lVal = 1;
SafeArrayPutElement (psa, & idx, & setdt );
Idx = 1;
Setdt. lVal = 2;
SafeArrayPutElement (psa, & idx, & setdt );
// Returns a security array.
V_VT (pVal) = VT_ARRAY | VT_VARIANT; // I didn't use VT_VARIANT here, but used the VT_I4 type. As a result, a new VBArray error occurs in JS Code. Please note that
V_ARRAY (pVal) = psa;
Return S_ OK;
}
JS Code:
<Script language = "JScript">
<! --
Function MyCall (){
Var obj = new VBArray (MyActiveX. TestStringArr ());
Var retVal = obj. toArray (); // use VBArray to convert a security array to a JS array.
Alert (retVallength );
// Document. write (retVal + "<br/> ")
Document. write (retVal. slice (1) + "<br/> ")
// Document. write (retVal)
}
-->
</SCRIPT>
You can see how to return the custom structure array later.
In fact, it can also return strings for js to parse, but this is slow, and the get is encapsulated into an xml string to return js parsing, etc.
From xt_chaoji's column