[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1218
  • Last Modified:

CHTMLView, View Source

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
MarkOttoSmith
Asked:
MarkOttoSmith
1 Solution
 
peterdownesCommented:
// 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
 
MarkOttoSmithAuthor Commented:
.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
 
job_sCommented:
#include "Atlbase.h"
for avoiding error due to ComPtr
0
 
MarkOttoSmithAuthor Commented:
cool it works.. but it doesnt give me the same as right clicking and view source...
0
 
MoondancerCommented:
Is more needed here?
Moondancer - EE Moderator
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now