Solved

CHTMLView, View Source

Posted on 2002-03-21
5
1,107 Views
Last Modified: 2013-11-20
I am using VC++6.0, Created a SDI App based on CHTMLVIEW

I use the following code to get & set the HTML source in the browser...


#include "mshtml.h"

CString CMyBrowserView::GetHTML()
{
     IHTMLDocument2* pDoc = (IHTMLDocument2*)GetHtmlDocument();
     IHTMLElement* pBody;
     pDoc->get_body ( &pBody );
     
     BSTR Body;
     pBody->get_outerHTML ( &Body );
     
     CString Page ( Body );
     ::SysFreeString (Body );

     return Page;
}

void CMyBrowserView::SetHTML(CString html)
{
     IHTMLDocument2* pDoc = (IHTMLDocument2*)GetHtmlDocument();
     
     IHTMLElement* pBody;
     pDoc->get_body ( &pBody );
     
     BSTR what = html.AllocSysString();
     pBody->put_innerHTML(what);
     ::SysFreeString (what);
}

But the above code only gets the body HTML and sets the body HTML.

What I need is a new function which gets the correct source.  Something that would get
the same source as when I would right click and go view source and it would open in notepad.. also like The IE->View Source

Also a fix on the setHTML function would be appreciated but not needed
0
Comment
Question by:MarkOttoSmith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 6

Accepted Solution

by:
peterdownes earned 300 total points
ID: 6887794
// Get document.
IHTMLDocument2* pDoc = (IHTMLDocument2*)GetHtmlDocument();

// Get element collection.
CComPtr<IHTMLElementCollection> p2;
   
VERIFY(SUCCEEDED(pDoc->get_all(&p2)));

// First element.
CComPtr<IDispatch>      p3;
CComQIPtr<IHTMLElement> p4;
CComBSTR                bstr;
CComVariant             var1(0);
CComVariant             var2(0);
long                    lLength = 0;

VERIFY(SUCCEEDED(p2->get_length(&lLength)));
VERIFY(SUCCEEDED(p2->item(var1, var2, &p3)));
VERIFY((p4 = p3) != NULL);
VERIFY(SUCCEEDED(p4->get_tagName(&bstr)));
VERIFY(bstr == "HTML");
VERIFY(SUCCEEDED(p4->get_outerHTML(&bstr)));

bstr contains your "View Source".

Regards
0
 

Author Comment

by:MarkOttoSmith
ID: 6890477
.cpp(352) : error C2065: 'CComPtr' : undeclared identifier
.cpp(352) : error C2275: 'IHTMLElementCollection' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\mshtml.h(28115) : see declaration of 'IHTMLElementCollection'
.cpp(352) : error C2065: 'p2' : undeclared identifier
.cpp(357) : error C2275: 'IDispatch' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\oaidl.h(2694) : see declaration of 'IDispatch'
.cpp(357) : error C2065: 'p3' : undeclared identifier
.cpp(358) : error C2065: 'CComQIPtr' : undeclared identifier
.cpp(358) : error C2275: 'IHTMLElement' : illegal use of this type as an expression
        C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\mshtml.h(10120) : see declaration of 'IHTMLElement'
.cpp(358) : error C2065: 'p4' : undeclared identifier
.cpp(359) : error C2065: 'CComBSTR' : undeclared identifier
.cpp(359) : error C2146: syntax error : missing ';' before identifier 'bstr'
.cpp(359) : error C2065: 'bstr' : undeclared identifier
.cpp(360) : error C2065: 'CComVariant' : undeclared identifier
.cpp(360) : error C2146: syntax error : missing ';' before identifier 'var1'
.cpp(360) : error C2065: 'var1' : undeclared identifier
.cpp(361) : error C2146: syntax error : missing ';' before identifier 'var2'
.cpp(361) : error C2065: 'var2' : undeclared identifier
.cpp(364) : error C2227: left of '->get_length' must point to class/struct/union
.cpp(365) : error C2227: left of '->item' must point to class/struct/union
.cpp(367) : error C2227: left of '->get_tagName' must point to class/struct/union
.cpp(368) : error C2446: '==' : no conversion from 'char *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
.cpp(368) : error C2040: '==' : 'int' differs in levels of indirection from 'char [5]'
.cpp(369) : error C2227: left of '->get_outerHTML' must point to class/struct/union
Error executing cl.exe.

22 error(s), 0 warning(s)

Didn't work
0
 
LVL 3

Expert Comment

by:job_s
ID: 6890745
#include "Atlbase.h"
for avoiding error due to ComPtr
0
 

Author Comment

by:MarkOttoSmith
ID: 6891774
cool it works.. but it doesnt give me the same as right clicking and view source...
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6973445
Is more needed here?
Moondancer - EE Moderator
0

Featured Post

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!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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