Recently about VC + + through the IDispatch invoke to achieve and JS Interactive do point TP, and the previously did not understand the "direct read JS variable value" and "pass a number of parameters" part of the re-implementation.
1, call JS function, and pass multiple parameters
ccomqiptr<ihtmldocument2> PDoc = M_browser.get_document (); if (PDOC) {ccomqiptr<idispatch> spDisp; HRESULT hr = Pdoc->get_script (&spdisp), if (SUCCEEDED (HR)) {//Function name to invoke tchar* Szmember = _t ("SetText");D ispid Dispi D HRESULT hr = Spdisp->getidsofnames (Iid_null, &szmember, 1,locale_system_default, &dispid); if (SUCCEEDED (HR ) {Dispparams dispparams;memset (&dispparams, 0, sizeof dispparams);d Ispparams.cargs = 2;//The parameters to be passed, it is strongly recommended to use the following form. When transferring a string, change to a BSTR. ccomvariant* pvars = new Ccomvariant[2]; PVARS[1] = L "1"; Pvars[0] = L "2"; Pvars[0]. ChangeType (VT_I4);p vars[1]. ChangeType (VT_I4);d ispparams.rgvarg = Pvars;dispparams.cnamedargs = 0; Excepinfo Excepinfo;memset (&excepinfo, 0, sizeof (excepinfo)); CComVariant Varesult; UINT NARGERR = (UINT)-1; Initialize to invalid ARGHR = Spdisp->invoke (DispID, Iid_null, Locale_system_default, dispatch_method,& Dispparams,&varesult,&excepinfo,&nargerr);}}}
2, directly read the JS variable (not by assigning value to a control and then reading the form)
ccomqiptr<ihtmldocument2> PDoc = M_browser.get_document (); if (PDOC) {ccomqiptr<idispatch> spDisp; HRESULT hr = Pdoc->get_script (&spdisp), if (SUCCEEDED (HR)) { //js variable name tchar* szmember = _t ("Nitem");D Ispid DISPID; HRESULT hr = Spdisp->getidsofnames (Iid_null, &szmember, 1,locale_system_default, &dispid); if (SUCCEEDED (HR ) {Dispparams dispparams;memset (&dispparams, 0, sizeof dispparams); Excepinfo Excepinfo;memset (&excepinfo, 0, sizeof (excepinfo)); CComVariant Varesult; UINT NARGERR = (UINT)-1; Initialize to invalid ARGHR = Spdisp->invoke (DispID, Iid_null, Locale_system_default, dispatch_propertyget,& Dispparams,&varesult,&excepinfo,&nargerr);}}}
Reference:
http://blog.csdn.net/hzlyh/article/details/5411086
Unfortunately, the JS variable directly assigned to the time, invoke has not been successful, but still need to continue to investigate .....
VC and JavaScript Interaction (ii)