?
Solved

Urgent: Trapping documentcomplete event in IE browser

Posted on 2006-05-04
10
Medium Priority
?
417 Views
Last Modified: 2013-11-20
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.

0
Comment
Question by:ranadhir
  • 6
  • 3
9 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 16611045
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
 

Author Comment

by:ranadhir
ID: 16620953
That does not solve the problem either;the documentcomplete seems not to be related to any of thebroswer state properties.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 450 total points
ID: 16623312
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ranadhir
ID: 16638576
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 16642517
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
 

Author Comment

by:ranadhir
ID: 16644315
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 16654078
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 16654138
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 16654260
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question