?
Solved

getting html file from IE control (WebBrowser2) in dialog based MFC application

Posted on 2004-11-03
6
Medium Priority
?
495 Views
Last Modified: 2013-11-20
Hi,

I have MFC dialog based application hosting IE control.
It successfuly displays any URL i want.

I'm trying to read html opened in IE control. I know there are few ways to do it - I've read exisitng posts and implemented solution using IPersistFile. It doesn't work.

Here's microsoft example for it :
http://support.microsoft.com/kb/q292485/

Here's my code :

void CTesterDlg::OnDocumentComplete(LPDISPATCH pDispatch, VARIANT FAR* URL)
{

    HRESULT          hr    = E_FAIL;
    IDispatch*       pDisp = NULL;
    IHTMLDocument2*  pDoc  = NULL;
      
    pDisp                  = m_browser.GetDocument();

      //pDisp = pDispatch;
      
      if(SUCCEEDED(hr = pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc)))
      {
            IPersistFile*      pFile      =      NULL;
            if(SUCCEEDED(pDoc->QueryInterface(IID_IPersistFile,(void**)&pFile)))
            {
                  LPCOLESTR      file = L"c:\\test1.htm";
                  pFile->Save(file,TRUE);
            }
      }
}


1) in debug mode i see that (LPDISPATCH pDispatch) which i get as parameter is different from
    pDisp = m_browser.GetDocument();.  Why is that ?

2) If i use LPDISPATCH pDispatch which i get as parameter , firts query fails.
 
    If i use    pDisp = m_browser.GetDocument()   (as shown in MS example) ,
    firts query succeded , but second query fails.  In debug mode i can see that pDoc equals to pDisp and that pFile is 0x000000 as it should be.


Any ideas why second query fails ?
0
Comment
Question by:c1727130
  • 4
6 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 800 total points
ID: 12482025
Searched result (from ShaunWilde)

void CBD::OnDocumentComplete(LPDISPATCH pDisp, LPCTSTR lpszURL)
{
     CComQIPtr<IWebBrowser2,&IID_IWebBrowser2> iWb = pDisp;
     HRESULT hr;
     CString szData; // this is where it will all end up
     if (iWb)
     {
          LPDISPATCH pDocDisp=NULL;
          hr=iWb->get_Document(&pDocDisp);
          if ((S_OK==hr) && pDocDisp)
          {
               CComQIPtr<IHTMLDocument2> iDoc = pDocDisp ;
               if (iDoc)
               {
                    // get the body element
                    IHTMLElement * pBodyElement=NULL;
                    iDoc->get_body(&pBodyElement);
                    if (pBodyElement)
                    {

                         CComBSTR szBody;
                         pBodyElement->get_outerHTML(&szBody);
                         // get the data
                         szData=szBody;
                         pBodyElement->Release();
                    }
                    iDoc.Release();
               }
               pDocDisp->Release();
          }
          iWb.Release();
     }
}

~roshan
0
 

Author Comment

by:c1727130
ID: 12484508
roshmon,

This code actually works , BUT..
The data saved to file by this code insn't identical to real content of loaded document  , in other words it's not the  same
as "view source".

The file i load into IE contol is XML file , and when i click view source i noly see few xml structures.
But , when i opened file saved this code , it has many additional XML tags.

What can be done in such case ?
0
 

Author Comment

by:c1727130
ID: 12484602
just correcting myself : many additional HTML tags.

example :

original:
--------------------------------------------------------------------------------
  <?xml version="1.0" encoding="UTF-8" ?>
- <MOB>
  <FUNCTION />
- <PARAMS>
  <RETCODE>1</RETCODE>
  <MESSAGE>Invalid Function</MESSAGE>
  </PARAMS>
  </MOB>
---------------------------------------------------------------------------




saved by above code :
---------------------------------------------------------------------------------------------------------
<BODY class=st><DIV class=e><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;?</SPAN><SPAN class=pi>xml version="1.0" encoding="UTF-8" </SPAN><SPAN class=m>?&gt;</SPAN> </DIV>

<DIV class=e>

<DIV class=c style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><A class=b onfocus=h() onclick="return false" href="#">-</A> <SPAN class=m>&lt;</SPAN><SPAN class=t>MOB</SPAN><SPAN class=m>&gt;</SPAN></DIV>

<DIV>

<DIV class=e>

<DIV style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>FUNCTION</SPAN> <SPAN class=m>/&gt;</SPAN> </DIV></DIV>

<DIV class=e>

<DIV class=c style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><A class=b onfocus=h() onclick="return false" href="#">-</A> <SPAN class=m>&lt;</SPAN><SPAN class=t>PARAMS</SPAN><SPAN class=m>&gt;</SPAN></DIV>

<DIV>

<DIV class=e>

<DIV style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>RETCODE</SPAN><SPAN class=m>&gt;</SPAN><SPAN class=tx>1</SPAN><SPAN class=m>&lt;/</SPAN><SPAN class=t>RETCODE</SPAN><SPAN class=m>&gt;</SPAN> </DIV></DIV>

<DIV class=e>

<DIV style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;</SPAN><SPAN class=t>MESSAGE</SPAN><SPAN class=m>&gt;</SPAN><SPAN class=tx>Invalid Function</SPAN><SPAN class=m>&lt;/</SPAN><SPAN class=t>MESSAGE</SPAN><SPAN class=m>&gt;</SPAN> </DIV></DIV>

<DIV><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;/</SPAN><SPAN class=t>PARAMS</SPAN><SPAN class=m>&gt;</SPAN></DIV></DIV></DIV>

<DIV><SPAN class=b>&nbsp;</SPAN> <SPAN class=m>&lt;/</SPAN><SPAN class=t>MOB</SPAN><SPAN class=m>&gt;</SPAN></DIV></DIV></DIV></BODY>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Assisted Solution

by:OnegaZhang
OnegaZhang earned 200 total points
ID: 12489689
try the following

                                    MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
                                    MSHTML::IHTMLElementPtr spHtmlElement;
                                    if(spHtmlDocument==NULL)
                                          continue;
                                    spHtmlDocument->get_body(&spHtmlElement);
                                    if(spHtmlDocument==NULL)
                                          continue;
                                    _bstr_t str ;
                                    str=spBrowser->GetLocationURL();//URL of IE window();
                                    AfxMessageBox(str);
                                    MSHTML::IHTMLDocument3* pHTMLDoc3;
                                    HRESULT hr = spHtmlDocument->QueryInterface(__uuidof(MSHTML::IHTMLDocument3),(LPVOID*)&pHTMLDoc3);
                                    ASSERT(SUCCEEDED(hr));
                                    MSHTML::IHTMLElement* pDocElem;
                                    hr = pHTMLDoc3->get_documentElement(&pDocElem);
                                    pHTMLDoc3->Release();
                                    ASSERT(SUCCEEDED(hr));
                                    BSTR bstrHTML;
                                    pDocElem->get_outerHTML(&bstrHTML);
                                    pDocElem->Release();
                                    AfxMessageBox(CString(bstrHTML));
                                    SysFreeString(bstrHTML);
0
 

Author Comment

by:c1727130
ID: 12491152
OnegaZhang ,

Thanks for your code - it works also.
Now , i think i understand what is the problem , but i dont know how to solve it.

1) Here's screenshot of  XML output as shown in my hosted WebBrowser control :
http://www.realmoney.co.il/temp/ie_content.gif

2) Here's screenshot of notepad , when clicking on "View source" of this hosted WebBrowser :
http://www.realmoney.co.il/temp/view_source.gif

and that's what i'm willing to get !!

3) Here's screenshot of buffer i get when executing code for extracting html content :
http://www.realmoney.co.il/temp/generated_html.gif


Now , the question is if it's possible to get same output as generated by "view source" ?!
Maybe i should executre the "View Source" command , copy to buffer content of Notepad and then close it ?

Your help very appreciated,
thanks
0
 

Author Comment

by:c1727130
ID: 12521685
I guess i should solve this by downloading file instead of navigating to it. Thanks for both of you , i'll split points.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

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