Interaction between VC and JavaScript (2) ---- call JS Functions

Source: Internet
Author: User

Interaction between VC and JavaScript (2) ---- call JS Functions

In this chapter, we will call JS functions by Using VC.

 

Let's write an HTML file that contains the following JavaScript code:

 

[Html]
  1. <Script type = text/javascript>
  2. Function Add (value1, value2 ){
  3. Return value1 + value2;
  4. }
  5. </Script>
    Then we load the HTML with WebBrowser and call the JS function named Add in VC as follows:

     

     

    [Cpp
    1. // Don't forget # include
    2. // M_WebBrowser is a WebBrowser Activex control object.
    3. CComQIPtr SpDoc = m_WebBrowser.get_Document ();
    4. CComDispatchDriver spScript;
    5. SpDoc-> get_Script (& spScript );
    6.  
    7. CComVariant var1 = 10, var2 = 20, varRet;
    8. SpScript. Invoke2 (LAdd, & var1, & var2, & varRet );

       


       

       

      SpScript. Invoke2 is used to call the Add function in the JS function. Two parameters are input and varRet is used to receive the return value.

      As you can see, varRet returns 30 after Invoke2 is called successfully.

       

      In this case, only one return value can be accepted at a time.

      What if I want to accept multiple return values at a time?

      We can let JS return an Array or Object in JS.

      When the JS function returns an Array or an Object, varRet on the VC side will receive an IDispatch interface representing the Object. We still use CComDispatchDriver to manage this IDispatch. Use the four methods of CComDispatchDriver described in the previous article:

      GetProperty

      GetPropertyByName

      PutProperty

      PutPropertyByName

      To retrieve the data from this Array or Object.

      Practice is the only criterion for testing truth. Let's write another JS function:

       

      [Html]
      1. <Script type = text/javascript>
      2. Function Add (value1, value2 ){
      3. Var array = new Array ();
      4. Array [0] = value1;
      5. Array [1] = value2;
      6. Array [2] = value1 + value2;
      7. Return array;
      8. }
      9. </Script>
        Then write in VC as follows:

         

         

        [Cpp]
        1. CComQIPtr SpDoc = m_WebBrowser.get_Document ();
        2. CComDispatchDriver spScript;
        3. SpDoc-> get_Script (& spScript );
        4.  
        5. CComVariant var1 = 10, var2 = 20, varRet;
        6. SpScript. Invoke2 (LAdd, & var1, & var2, & varRet );
        7.  
        8. CComDispatchDriver spArray = varRet. pdispVal;
        9. // Obtain the number of elements in the Array. This length is the attribute of the Array object in JS.
        10. CComVariant varArrayLen;
        11. SpArray. GetPropertyByName (Llength, & varArrayLen );
        12. // Obtain the value of element 0, 1 and 2 in the array:
        13. CComVariant varValue [3];
        14. SpArray. GetPropertyByName (L0, & varValue [0]);
        15. SpArray. GetPropertyByName (L1, & varValue [1]);
        16. SpArray. GetPropertyByName (L2, & varValue [2]);

           


           

           

           

          We can see that the values returned by these three JS functions are already in our varValue [3.

          Of course, if we do not know that the Array object returned by JS contains several elements, we can obtain its length attribute on the VC side, and then retrieve each value in the Array in a loop.

           

          If our JS function returns an Object that contains multiple attribute values, how can VC receive this Object?

          Let's write another JS function:

           

          [Html]
          1. <Script type = text/javascript>
          2. Function Add (value1, value2 ){
          3. Var data = new Object ();
          4. Data. result = value1 + value2;
          5. Data. str = Hello, I'm James !;
          6. Return data;
          7. }
          8. </Script>
            Then in VC, we receive the following message:

             

             

            [Cpp]
            1. CComQIPtr SpDoc = m_WebBrowser.get_Document ();
            2. CComDispatchDriver spScript;
            3. SpDoc-> get_Script (& spScript );
            4.  
            5. CComVariant var1 = 10, var2 = 20, varRet;
            6. SpScript. Invoke2 (LAdd, & var1, & var2, & varRet );
            7.  
            8. CComDispatchDriver spData = varRet. pdispVal;
            9. CComVariant varValue1, varValue2;
            10. SpData. GetPropertyByName (Lresult, & varValue1 );
            11. SpData. GetPropertyByName (Lstr, & varValue2 );

               


               

               

              The two attributes of the Object returned by JS are result and str, which are an integer data and a string respectively.

              Here, the JS Code is written by ourselves. On the VC side, of course, we know that the objects returned by this JS function have the attributes "result" and "str.

              What should I do if the JS Code is not written by us or its attributes are uncertain in advance? The answer is to use the IDispatchEx interface to enumerate related information (method name and attribute name) of this object ).

              I will not talk about this for the time being, but will talk about it in subsequent articles.

               

              Of course, JS can not only return Object objects, but also return any objects. when an Object is returned rather than the basic data type (integer, floating point, string, the return value received by VC is an IDispatch. Then, we need to call the GetPropertyByName method to retrieve its attributes from the Objects represented by this IDispatch.

               

              In this way, VC calls the JS function and passes the parameters to JS and JS to return the return value to VC, which will be roughly the same.

              If you are not familiar with the smart variable VARIANT packaged by CComVariant, you can view relevant information online. In-depth analysis of ATL and other books are introduced.

               

              It is worth noting that the smart packaging classes starting with CCom provided by ATL do not depend on the dynamic library of ATL. Because I didn't select the link to ATL in the VC project, there were no modules such as ATL100.dll loaded during program debugging. You can use it without worrying about relying on ATL.

               

              VC can call JS functions. So how do JS functions call VC? We will try again later in the next article.

Related Article

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.