改写HTML代码后,代码一直在OnDownloadBegin, OnNavigateComplete2,OnDocumentComplete间死循环,没有正常显示当我打开一个网页后,在OnDocumentComplete 中用IID_IPersistStreamInit的Load方法改写了里面的东西。
但是浏览器就不正常了,显示'about:blank". 而且代码一直在OnDownloadBegin, OnNavigateComplete2,OnDocumentComplete间死循环。
我的代码如下,请大侠给些建议。我尝试了几种代码。
代码1
void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSourceNew;
GetHtml(csHtmlSourceNew);
SetHtml( csHtmlSourceNew ) ;
CHtmlView::OnDocumentComplete(lpszURL);
}
代码2
void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSource;
GetHtml(csHtmlSourceNew);
SetHtml( csHtmlSource ) ;
//CHtmlView::OnDocumentComplete(lpszURL);
}代码3void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSource;
GetSource(csHtmlSource );
SetHtml( csHtmlSource ) ;
CHtmlView::OnDocumentComplete(lpszURL);
}
代码4
void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSource;
GetSource(csHtmlSource );
SetHtml( csHtmlSource ) ;
//CHtmlView::OnDocumentComplete(lpszURL);
}获取和保存HTML网页的代码
BOOL CSmartView::GetHtml(CString &strHtml)
{
LPSTREAM pStream = NULL;
HGLOBAL hHTMLText = NULL;
IPersistStreamInit *psi = NULL;
LPDISPATCH pDisp = GetHtmlDocument( );
if (pDisp == NULL)
return FALSE;
IHTMLDocument2 *pDoc = NULL;
HRESULT hr = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
pDisp->Release();
if (hr != S_OK)
return FALSE;
hr = pDoc->QueryInterface(IID_IPersistStreamInit, (void**)&psi);
pDoc->Release();
if (hr != S_OK)
return FALSE;
// _ULARGE_INTEGER size;
// psi->GetSizeMax(&size);
// hHTMLText=GlobalAlloc(GPTR, size);
BOOL bSuc = FALSE;
hHTMLText = GlobalAlloc(GPTR, 0x80000);
if (hHTMLText != NULL)
{
if (CreateStreamOnHGlobal(hHTMLText, true, &pStream) == S_OK)
{
if (psi->Save(pStream, false) == S_OK)
{
bSuc = TRUE;
strHtml=(LPTSTR)hHTMLText;
}
pStream->Release();
}
GlobalFree(hHTMLText);
}
psi->Release();
return bSuc;
}
BOOL CSmartView::SetHtml(LPCTSTR lpszHtml)
{
LPSTREAM pStream = NULL;
HGLOBAL hHTMLText = NULL;
IPersistStreamInit *psi = NULL;
LPDISPATCH pDisp = GetHtmlDocument( );
if (pDisp == NULL)
return FALSE;
IHTMLDocument2 *pDoc = NULL;
HRESULT hr = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
pDisp->Release();
if (hr != S_OK)
return FALSE;
hr = pDoc->QueryInterface(IID_IPersistStreamInit, (void**)&psi);
pDoc->Release();
if (hr != S_OK)
return FALSE;
BOOL bSuc = FALSE;
if (psi->InitNew() == S_OK)
{
int len = _tcslen(lpszHtml)+256;
hHTMLText = GlobalAlloc(GPTR, len * sizeof(TCHAR));
if (hHTMLText != NULL)
{
_tcscpy((LPTSTR)hHTMLText, lpszHtml);
if (CreateStreamOnHGlobal(hHTMLText, true, &pStream) == S_OK)
{
if (psi->Load(pStream) == S_OK)
bSuc = TRUE;
pStream->Release();
}
else
{
GlobalFree(hHTMLText);
}
}
}
psi->Release();
return bSuc;
}
但是浏览器就不正常了,显示'about:blank". 而且代码一直在OnDownloadBegin, OnNavigateComplete2,OnDocumentComplete间死循环。
我的代码如下,请大侠给些建议。我尝试了几种代码。
代码1
void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSourceNew;
GetHtml(csHtmlSourceNew);
SetHtml( csHtmlSourceNew ) ;
CHtmlView::OnDocumentComplete(lpszURL);
}
代码2
void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSource;
GetHtml(csHtmlSourceNew);
SetHtml( csHtmlSource ) ;
//CHtmlView::OnDocumentComplete(lpszURL);
}代码3void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSource;
GetSource(csHtmlSource );
SetHtml( csHtmlSource ) ;
CHtmlView::OnDocumentComplete(lpszURL);
}
代码4
void CSmartView::OnDocumentComplete(LPCTSTR lpszURL)
{
// TODO: Add your specialized code here and/or call the base class
CString csHtmlSource;
GetSource(csHtmlSource );
SetHtml( csHtmlSource ) ;
//CHtmlView::OnDocumentComplete(lpszURL);
}获取和保存HTML网页的代码
BOOL CSmartView::GetHtml(CString &strHtml)
{
LPSTREAM pStream = NULL;
HGLOBAL hHTMLText = NULL;
IPersistStreamInit *psi = NULL;
LPDISPATCH pDisp = GetHtmlDocument( );
if (pDisp == NULL)
return FALSE;
IHTMLDocument2 *pDoc = NULL;
HRESULT hr = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
pDisp->Release();
if (hr != S_OK)
return FALSE;
hr = pDoc->QueryInterface(IID_IPersistStreamInit, (void**)&psi);
pDoc->Release();
if (hr != S_OK)
return FALSE;
// _ULARGE_INTEGER size;
// psi->GetSizeMax(&size);
// hHTMLText=GlobalAlloc(GPTR, size);
BOOL bSuc = FALSE;
hHTMLText = GlobalAlloc(GPTR, 0x80000);
if (hHTMLText != NULL)
{
if (CreateStreamOnHGlobal(hHTMLText, true, &pStream) == S_OK)
{
if (psi->Save(pStream, false) == S_OK)
{
bSuc = TRUE;
strHtml=(LPTSTR)hHTMLText;
}
pStream->Release();
}
GlobalFree(hHTMLText);
}
psi->Release();
return bSuc;
}
BOOL CSmartView::SetHtml(LPCTSTR lpszHtml)
{
LPSTREAM pStream = NULL;
HGLOBAL hHTMLText = NULL;
IPersistStreamInit *psi = NULL;
LPDISPATCH pDisp = GetHtmlDocument( );
if (pDisp == NULL)
return FALSE;
IHTMLDocument2 *pDoc = NULL;
HRESULT hr = pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc);
pDisp->Release();
if (hr != S_OK)
return FALSE;
hr = pDoc->QueryInterface(IID_IPersistStreamInit, (void**)&psi);
pDoc->Release();
if (hr != S_OK)
return FALSE;
BOOL bSuc = FALSE;
if (psi->InitNew() == S_OK)
{
int len = _tcslen(lpszHtml)+256;
hHTMLText = GlobalAlloc(GPTR, len * sizeof(TCHAR));
if (hHTMLText != NULL)
{
_tcscpy((LPTSTR)hHTMLText, lpszHtml);
if (CreateStreamOnHGlobal(hHTMLText, true, &pStream) == S_OK)
{
if (psi->Load(pStream) == S_OK)
bSuc = TRUE;
pStream->Release();
}
else
{
GlobalFree(hHTMLText);
}
}
}
psi->Release();
return bSuc;
}
CString csHtmlSourceTest( _T("<html><h1>Stream Test</h1><p>This HTML content is being loaded from a stream.</html>") ); SetHtml( csHtmlSourceTest ) ;
CString csHtmlSourceTestChk;
CString csHtmlSourceTestChk2;
GetHtml(csHtmlSourceTestChk);
GetSource(csHtmlSourceTestChk2);
if( csHtmlSourceTestChk != csHtmlSourceTest )
{
log_notew( _T("OnDocumentComplete SetHtml failed.") );
}我在设置后马上取回的HTML文档变成了<p> </p>。 但是下一次调用的OnNavigateComplete2, OnDownloadComplete取到的文档就是对的。
请问哪儿出了问题?