Solved

CHTMLView, View Source

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven archtype selection in eclipse 1 57
post4 challenge 28 104
mapAB Challlenge 35 155
Problem to App 4 97
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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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