VC ++ access JavaScript (4)-the original function is also an object

Source: Internet
Author: User

We already know how to use VC ++ to access and call functions in JavaScript. So how can we implement a function in VC ++ so that it can be called in JavaScript? For example, we use VC ++ to provide a function to bind an onclick event of a webpage element. When the event arrives, we can call this function in VC ++.

In JavaScript, functions are also an object. From the perspective of VC ++, when we call a function, we actually call the 0 method (that is, the default method) of the function object ). What is the 0 method? All objects are assigned a unique dispid for all the members (including all attributes and methods) managed by themselves. When a method of an object is called, its dispid is obtained through the method name, then, use dispid to call the invoke method of the object (if the idispatchex interface is provided, the invokeex method is called ). When the dispid of a member is equal to 0, it is the default attribute or default method of the object (we call it the 0 method ). The advantage of the 0 method is that in actual calls, we do not need to specify the name of the 0 method to complete the call. If you have used vbprogramming, you will know that the 0 attribute or the 0 method is a common usage.

Therefore, we can use VC ++ to implement a COM Object, inherit from the idispatch interface, provide a 0 method, and then bind the onclick event of the webpage element with this COM object. In addition, to make this COM Object support calls with the this pointer, it must inherit from the idispatchex interface. In practice, events bound to web elements only need to inherit from the idispatch interface.

 

Next, we will implement a COM object with the 0 Method

  1. // Ivcfunction
  2. [
  3. Object,
  4. UUID ("E0B4E698-945D-4CD5-BF8B-ECE65DA39DFE "),
  5. Dual, helpstring ("ivcfunction interface "),
  6. Pointer_default (unique)
  7. ]
  8. _ Interface ivcfunction: idispatch
  9. {
  10. };
  11. // Cvcfunction
  12. [
  13. Coclass,
  14. Default (ivcfunction ),
  15. Threading (apartment ),
  16. Vi_progid ("pimshelldemo. vcfunction "),
  17. Progid ("pimshelldemo. vcfunction.1 "),
  18. Version (1.0 ),
  19. UUID ("F4BA402B-3A59-49CC-85A1-12E91E5A0F99 "),
  20. Helpstring ("vcfunction class ")
  21. ]
  22. Class atl_no_vtable cvcfunction:
  23. Public idispatchimpl <ivcfunction>
  24. {
  25. Public:
  26. Cvcfunction ()
  27. {
  28. }
  29. Declare_protect_final_construct ()
  30. Hresult finalconstruct ()
  31. {
  32. Return s_ OK;
  33. }
  34. Void finalrelease ()
  35. {
  36. }
  37. Public:
  38. Stdmethod (invoke) (dispid dispidmember, refiid riid, lcid, word wflags,
  39. Dispparams * pdispparams, variant * pvarresult,
  40. Raise info * p1_info, uint * puargerr)
  41. {
  42. // Only the 0 method is supported
  43. If (dispidmember! = 0)
  44. Return e_invalidarg;
  45. // If an event is bound to a webpage element through attachevent, the first input parameter is the event object.
  46. Mshtml: ihtmleventobjptr pevent = pdispparams-> rgvarg [0]. pdispval;
  47. // Event. Type
  48. _ Bstr_t bstrtype = pevent-> type;
  49. //
  50. If (: wcscmp (bstrtype, l "click") = 0)
  51. {
  52. // Execute the code in response to the onclick event
  53. }
  54. Return s_ OK;
  55. }
  56. };

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.