Urgent: Trapping documentcomplete event in IE browser

We have an ATL application which hosts an IE browser.
We create the browser instance and subscribe to it as follows:

STDMETHODIMP CIEdispatcher::createIE(void)
{
// TODO: Add your implementation code here
HRESULT hr = E_FAIL;
if (m_spIE == NULL) // Can only start one instance of IE4
{
// Create an instance of IE4 and get the IWebBrowser2 interface pointer.
// IWebBrowser2 represents the application as well as the WebBrowser control.
hr = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,//CLSCTX_INPROC_SERVER,//
IID_IWebBrowser2, (void**)&m_spIE);


if (SUCCEEDED(hr))
{
m_spIE->put_Visible(TRUE); // We want to see IE4 don't we???
cout<< " Registering for browser events " << endl;
AtlAdvise(m_spIE,GetUnknown(),DIID_DWebBrowserEvents2,&m_dwCookie);
CComVariant vurl(_T("http://www.yahoo.com"));
hr = m_spIE->Navigate2(&vurl, &ve, &ve, &ve, &ve);
}
return S_OK;
}

However the program exists before the documentcomplete is fired!!!So we try to capture the readystate property before returning from the method as follows:

READYSTATE result;


do
{
m_spIE->get_ReadyState (&result);

} while (result != READYSTATE_COMPLETE);

But even then the program exits before documentcomplete is fired.
However if we keep the program running indefinitely,documentcomplete is fired ,and we can load the document tree.

What should we do to prevent the program from exitting before documentcomplete is fired? We have also tried get_statustext as Done with no avail.

ranadhirAsked:
Who is Participating?
 
DanRollinsCommented:
If the page is actually a FRAMESET, that migth screw up the works.
I seem to recall having no difficulty using either Busy or ReadyState.  However, if the only thing you have is DocumentComplete, then you can just have your DocumenrComplete handler set a flag.  Then in the calling fn, just have it wait for that flag to get set.  E.g.,

BOOL gfDone;

CMyWebWindow::DocumentComplete()
}
    ...Do all processing...
    gfDone= TRUE;
}

main() {
    ...
    gfDone= FALSE:
    ...
    hr = m_spIE->Navigate2(&vurl, &ve, &ve, &ve, &ve);
    while ( !gfDone ) {
        Sleep(1000);
    }
     return(0); // exit the program
}

Some related sample code here:
    How To Determine When a Page Is Done Loading in WebBrowser Control
    http://support.microsoft.com/kb/q180366/
0
 
DanRollinsCommented:
Try looping, waiting for BUSY to be done...
   
do
{
     Sleep(100);
} while ( fResult= m_spIE->get_Busy( &fResult )  );

Note:  I recommend adding some code to provide a failsfe timeout incase of some disaster (you don't want to sit there forever if Busy never goes FALSE).

-- Dan
0
 
ranadhirAuthor Commented:
That does not solve the problem either;the documentcomplete seems not to be related to any of thebroswer state properties.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ranadhirAuthor Commented:
In this case the browser just hangs .It completes the naigation only after we exit out of the application.i have even tried creating the broswer instance in a separate thread but to no avail.It just fires the beforenavigate event and waits for this client application to exit.i am creatign an out-of -proc IE instance.
0
 
DanRollinsCommented:
Just so I understand:
Are you saying that you never get a DocumentComplete event, even if you wait for several minutes or forever?

Does this happen only with a certain webpage, but not other ones, such as Yahoo or EE?
0
 
ranadhirAuthor Commented:
yes.the browser just does not complete the navigation till i exit out of my client console application.Only then does it finish the navigation,and hence the subsequebt events.
This is not a problem in MFC GUI based applications where the navigation is done by click of a button or ome such event.
But in a console based application,I just cant get teh browser to complete the navigation,unless ithe console application exits.
0
 
DanRollinsCommented:
The fact that you have written a CONSOLE application is probably the critical issue... I wish you had mentioned it earlier.  I'll take a look and see if I can reproduce it.  If I don't get back to you by tomorrow, "ping" me by posting to this thread again.

-- Dan
0
 
DanRollinsCommented:
I think I found it in a google serach.  You muyst need to pump messages.  Form this Google Groups thread:
http://groups.google.com/group/borland.public.delphi.language.delphi.win32/browse_thread/thread/2f081135030b038a/3e085558c6db6367

?lnk=st&q=CLSID_InternetExplorer+get_ReadyState&rnum=1&hl=en#3e085558c6db6367
... i found this sequence...

        READYSTATE ready;
        time_t stoptime = time(NULL) + 60;
        while (true)
        {
                MSG msg;
                if( ::PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE ) )
                {
                        ::TranslateMessage( &msg );
                        ::DispatchMessage( &msg );
                }
                if ((wb->get_ReadyState(&ready) == S_OK && ready ==
READYSTATE_COMPLETE) ||
                        time(NULL) > stoptime) break;
        }


It looks like that will fix you right up.  Give it a try.
-- Dan
0
 
DanRollinsCommented:
Incidently, if you goalr is to just download the HTML of a page, you might consider using IXMLHTTPRequest (even though it's not about the XML, that object can download any resource).  There is a C++ example here:
     http://msdn.microsoft.com/library/en-us/xmlsdk/html/63409298-0516-437d-b5af-68368157eae3.asp

You can also consuder using
  URLDownloadToFile
  http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/moniker/reference/functions/urldownloadtofile.asp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.