Javascript retrieves the VBArray array (integer or string array) returned by COM or ATL.

Source: Internet
Author: User

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

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.