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
- // Ivcfunction
- [
- Object,
- UUID ("E0B4E698-945D-4CD5-BF8B-ECE65DA39DFE "),
- Dual, helpstring ("ivcfunction interface "),
- Pointer_default (unique)
- ]
- _ Interface ivcfunction: idispatch
- {
- };
- // Cvcfunction
- [
- Coclass,
- Default (ivcfunction ),
- Threading (apartment ),
- Vi_progid ("pimshelldemo. vcfunction "),
- Progid ("pimshelldemo. vcfunction.1 "),
- Version (1.0 ),
- UUID ("F4BA402B-3A59-49CC-85A1-12E91E5A0F99 "),
- Helpstring ("vcfunction class ")
- ]
- Class atl_no_vtable cvcfunction:
- Public idispatchimpl <ivcfunction>
- {
- Public:
- Cvcfunction ()
- {
- }
- Declare_protect_final_construct ()
- Hresult finalconstruct ()
- {
- Return s_ OK;
- }
- Void finalrelease ()
- {
- }
- Public:
- Stdmethod (invoke) (dispid dispidmember, refiid riid, lcid, word wflags,
- Dispparams * pdispparams, variant * pvarresult,
- Raise info * p1_info, uint * puargerr)
- {
- // Only the 0 method is supported
- If (dispidmember! = 0)
- Return e_invalidarg;
- // If an event is bound to a webpage element through attachevent, the first input parameter is the event object.
- Mshtml: ihtmleventobjptr pevent = pdispparams-> rgvarg [0]. pdispval;
- // Event. Type
- _ Bstr_t bstrtype = pevent-> type;
- //
- If (: wcscmp (bstrtype, l "click") = 0)
- {
- // Execute the code in response to the onclick event
- }
- Return s_ OK;
- }
- };