Solved

CHTMLView, View Source

Posted on 2002-03-21
5
1,022 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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: 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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now