Recently modified the old MFC project, with the Web interface, and HTML interactive use of the COM method, cumbersome, ugly and low performance, so consider using JS to interface interaction, check the information of the day, now collated as follows, for later needs of the people reference, Although almost no one will use MFC to do the project (laughter), with WinForm or WPF or Web interface, high-performance parts with C + + encapsulated DLL may be a better choice, of course, directly in the EXE to provide HTTP access interface is the most roar (go language race high)! Interface This is a relatively large topic, you can open a topic slowly said ...
The core of a Cwebpage class is written by a foreigner, encapsulated a number of COM operations, and WebBrowser to make a JS call, but can only invoke the existing JS code, sometimes need to inject some JS function to the Web page (you know), you can be on a blank page, Use JS to generate the entire page.
ihtmldocument2* Phtmldoc = (ihtmldocument2*) web.get_document (); if(Phtmldoc! =NULL) {IHTMLWindow2*Phtmlwnd; Phtmldoc->get_parentwindow (&Phtmlwnd); if(Phtmlwnd! =NULL) {CString Js_code; Js_code="function Test (str) {alert (str); return str;}";//This is the actual JavaScript code that was written//Js_code. Format (L "gotolocation ('%s ');", Jscode);//JS function name + parameterVARIANT ret; Phtmlwnd->execscript (Js_code. AllocSysString (), L"Javascript", &ret);//executing javascript codePhtmlwnd->Release (); Cwebpage page; Page. Setdocument (Web.get_document ()); CComVariant Varresult; Page. Calljscript ("Test","123",&varresult); MessageBox (CString (varresult.bstrval)); } Phtmldoc-Release (); }
First injection to the Web page (this sounds taller) a JS function (code is only one of the methods, and other methods are not enumerated), and then through the webpage encapsulated method call function and return the value, this will be more than C + + with COM interface operation Web page simple and faster (and no test, Theoretically it should be)
Here just to solve the MFC call Js,js call MFC is more complex and do not go deep, com this thing feels really ancient relics, the project is to intercept the page's jump address to determine the need to carry out the operation, basically enough, Remember WinForm WebBrowser and JS call each other is to encapsulate the object into a COM object exposed to webbrowser.
Source: Download
MFC interacts with WebBrowser (via JS)