First: called by Execscript. This method, while convenient to operate, cannot get the return value.
M_sphtmldoc->get_parentwindow (&m_phtmlwindow);
VARIANT ret;
RET.VT = Vt_empty;
BSTR BSTR = sscript.allocsysstring ();
BRet = M_phtmlwindow->execscript (BSTR, L "JavaScript", &ret);:
: SysFreeString (BSTR);
Sret = CString (ret);
The second: first use GetIDsOfNames to find the script function name, and then call. This method can return the result with a return value. However, you cannot call JS's system functions, such as Eval.
BOOL cdhtmldlgwindow::calljscript (const CString strfunc, const cstringarray& ParamArray, ccomvariant* pvarresult) {
CComPtr Spscript;
if (Null==m_sphtmldoc) {return FALSE;
HRESULT hr;
hr = M_sphtmldoc->get_script (&spscript); if (!
SUCCEEDED (HR)) {return FALSE;
} CComBSTR Bstrmember (Strfunc);
DISPID DISPID = NULL;
hr = Spscript->getidsofnames (iid_null,&bstrmember,1, locale_system_default,&dispid);
if (FAILED (HR)) {return FALSE;
const int arraysize = Paramarray.getsize ();
Dispparams Dispparams;
memset (&dispparams, 0, sizeof dispparams);
Dispparams.cargs = ArraySize;
Dispparams.rgvarg = new Variant[dispparams.cargs]; for (int i = 0; i < arraysize i++) {CComBSTR BSTR = Paramarray.getat (ArraySize-1-i);//Back reading BS Tr.
CopyTo (&dispparams.rgvarg[i].bstrval);
DISPPARAMS.RGVARG[I].VT = VT_BSTR;
} Dispparams.cnamedargs = 0;
Excepinfo Excepinfo;memset (&excepinfo, 0, sizeof excepinfo);
CComVariant Varesult; UINT Nargerr = (UINT)-1; Initialize to invalid arg hr = Spscript->invoke (dispid,iid_null,0, Dispatch_method,&dispparam
S,&VARESULT,&EXCEPINFO,&NARGERR);
delete [] dispparams.rgvarg;
if (FAILED (HR)) {return FALSE;
} *pvarresult = Varesult;
return TRUE; }
When actually used, it is possible to first access (Navigate) a page. Then, in VC inside the page for some JS call, and retrieve the results. It is possible that this JS call function is not in this page. It is generally possible to call an eval (some JS statements) in the form of a function that is not in the page, but neither of the above methods supports eval.
Third: Get the current document context through IScriptControl, and then call the Iscriptcontrol::raw_eval operation. (Only with Raw_eval, the Eval method prompts for no permissions.) )
First define a: Iscriptcontrolptr, and then in OnDocumentComplete inside call M_sphtmldoc->get_parentwindow (&m_phtmlwindow);
Iscriptcontrolptr->addobject ("Window", M_phtmlwindow, VARIANT_TRUE);
The third requires the import of "MSScript.ocx"
The above VC call JavaScript several methods (recommended) is small to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.