internet explorer - Webbrowser Control - displaying text using "write" -


i'm using iwebbrowser2 interface render page html string created @ runtime. have written method (let's call displayhtmlstring) takes html string , renders shown in this example. method calls navigate2 "about:blank" first, ensure document present, , calls close after calling write.

the first time call displayhtmlstring, page rendered correctly, i.e. browser displays html according string pass. problem subsequent calls not work correctly, render blank page instead. causing this?

i have found out when blank page shown, result of navigating about:blank. determined navigating local file instead, shown (whereas html string should shown instead, due subsequent write/close). call navigate2 works, while calls write , close don't.

i considered ie-internal security checks possible cause (cross-domain checking?), gut feeling isn't what's happening here.

it seems more me kind of synchronization problem, along lines of "ie hasn't finished rendering yet before next call displayhtmlstring comes along". code didn't check browser's ready state (because example doesn't). added experimental waiting loop call get_readystate , observed state never got beyond "loading" before returning method - because rendering asynchronous(?). notice when successive calls displayhtmlstring work correctly, program's main message loop has run (giving windows chance process messages), not case in scenario successive calls displayhtmlstring fail.

so i'm pretty sure need provide correct synchronization here, how? notice there's method named onreadystatechange, haven't experimented yet, due multitude of other things tried while groping in dark. solution, , how 1 use correctly? or, alternatively, shall process message loop inside displayhtmlstring until ready state changes "complete"?

update: added message loop processing displayhtmlstring. in first call (which works), ready state gets "interactive", no further (which doesn't seem problem). in subsequent call (when fails), ready state stays @ "loading", though message loop processed.

you should handle readystatechange event on document object. in javascript, this:

<body> <body>hi, going replaced!</body> <script> window.onload = function() {     document.open("text/html");     document.onreadystatechange = function() {          if (document.readystate == "complete")             alert("done!");      }     document.write("<b>hello again!</b>");     document.close();    } </script> </body> 

to done c++ or c#, easiest way provide implementation of idispatch interface ihtmldocument2::put_readystatechange. idispatch::invoke(dispid_value) called upon readystatechange event.

i might able code sample if specify language in use.

[edited] can grab complete example (c++/atl/vs2012) here. code asynchronously posting custom message main window. it's long cite here, below relevant parts.

idispatch implementation, onreadystatechange event sink:

class ceventsink:    public ccomobjectroot,   public idispatch { private:   hwnd m_hwnd;   uint m_message;  public:   ceventsink()   {     m_hwnd = null;     m_message = null;   }    begin_com_map(ceventsink)     com_interface_entry(idispatch)   end_com_map()    // init   void init(hwnd hwnd, uint message)   {     m_hwnd = hwnd;     m_message = message;   }    // idispatch   stdmethodimp gettypeinfocount(uint* pctinfo) {      return e_notimpl; }    stdmethodimp gettypeinfo(uint itinfo, lcid lcid, itypeinfo** pptinfo) {     return e_notimpl; }    stdmethodimp getidsofnames(refiid riid, lpolestr* rgsznames, uint cnames, lcid lcid, dispid* rgdispid) {      return e_notimpl; }    stdmethodimp invoke(     dispid dispidmember, refiid riid, lcid lcid, word wflags,     dispparams* pdispparams, variant* pvarresult,     excepinfo*  pexcepinfo, uint* puargerr)   {     if ( dispidmember != null )       return disp_e_membernotfound;      // post message notify main window     ::postmessage(m_hwnd, m_message, 0, 0);     return s_ok;   }  }; 

using it:

ccomobject<ceventsink>* p = null; hr = ccomobject<ceventsink>::createinstance(&p); if ( failed(hr) )    return 0; p->init(m_hwnd, wm_documentreadystatechange); m_eventsink = p; // addref  // ...  m_htmldocument2->put_onreadystatechange(ccomvariant(m_eventsink)); 

for more details, the sources , @ webochost.cpp. error checks basic, brevity.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -