Click to See Complete Forum and Search --> : Getting HTML Source Code To CString


Bono
August 31st, 1999, 11:00 AM
Does anybody have an idea of how to retrieve HTML Source Code from Microsoft WebBrowser control in VC++ 6.0 to CString.

Thanks
Svenni Bono.

LJP
August 31st, 1999, 12:25 PM
with this- it downloads the html file, and puts it in the CString name

this came from the VC++ documentation

void CWebBrowseView::OnViewSource()
{
HANDLE heMail;
DWORD dwWritten;

CString url = GetLocationURL();
HINTERNET hSession;
hSession = InternetOpen("llornkcor browser", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (hSession)
{
HINTERNET hService;
hService = InternetOpenUrl(hSession, url, NULL,
0, 0, 0);
if (hService)
{
DWORD dwBytesAvailable, dwBytesRead;
InternetQueryDataAvailable(hService, &dwBytesAvailable, 0, 0);
char *lpBuffer = (char*)malloc(dwBytesAvailable+1);
InternetReadFile(hService, lpBuffer, dwBytesAvailable, &dwBytesRead);
lpBuffer[dwBytesRead]=NULL;
CString name = lpBuffer;
try
{
heMail = CreateFile ("C:\\llornkcor\\tmp.txt",
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL);

WriteFile (heMail,
name,
strlen(name),
&dwWritten, NULL);
CloseHandle(heMail);
}
catch(...)
{
}


ShellExecute(m_hWnd,
"open",
"C:\\llornkcor\\tmp.txt",
NULL,
NULL,
SW_SHOWNORMAL);

// AfxMessageBox(lpBuffer);
free(lpBuffer);
}
}

Martin Speiser
August 31st, 1999, 02:03 PM
Hi Svenni,

if you have hosted the WebBrowser control query the interface IHTMLDocument2, call the toString function, and convert the resulting BSTR to a CString. You can do it by casting it to _bstr_t object and use the extractor char*.


Martin

September 24th, 1999, 09:43 AM
I tried this, but the BSTR returned contains "[object]" only.
Any idea? Can you help one again?
Peter

Martin Speiser
September 24th, 1999, 07:14 PM
Hi Peter,

Bono wrote private to me, with the same question. I was thinking too fast, sorry for that. Here's a solution for IE5:

void CShowSourceView::OnShowSource()
{
IHTMLDocument2Ptr pDocument = GetHtmlDocument();
IHTMLElementCollectionPtr pCollection;
IDispatchPtr pDispatch;
if ( ( S_OK == pDocument->get_all( &pCollection ) ) &&
( NULL != pDocument ) &&
( S_OK == pCollection->tags( _variant_t( "HTML" ), &pDispatch ) ) &&
( NULL != pDispatch ) )
{
IHTMLElementCollectionPtr pCollectionHTML = pDispatch;
if ( pCollectionHTML != NULL )
{
IDispatchPtr pDispatchHTML;
if ( S_OK == pCollectionHTML->item( _variant_t( 0L ), _variant_t(), &pDispatchHTML ) )
{
IHTMLElementPtr pHTML = pDispatchHTML;
if ( NULL != pHTML )
{
BSTR bstrHTML = NULL;
if ( S_OK == pHTML->get_outerHTML( &bstrHTML ) )
{
CString strHTML = bstrHTML;
::SysFreeString( bstrHTML );
}
}
}
}
}
}




Martin

September 28th, 1999, 04:06 AM
great you take care - but your code works not under IE4 (get_outerHTML returns E_FAIL, happens always if I try to get access to the HEAD tag).
Do you have any idea to get the thing running under IE4 ? I don't want to require IE5 for the app.

Perhaps you know another way - my original problem is: load a HTML, modify it through IHTMLxxxxx, and save the modified contents. However, if I use IPersistStreamInit, or IPersistFile, only the ORIGINAL source (without my modifications) is saved. (If the document is modified by some DHTML script, IPersist works fine)
Similar behaviour with document->ExecWB

Hope you can help

Peter

Martin Speiser
September 28th, 1999, 05:53 AM
Hi Peter,

as I wrote, it will work only under IE5. IE4 doesn't support get_outerHTML for the <HTML> tag.

And sorry, I never worked with the IPersistxxx interfaces.

Martin