block a website with bho give an error

Hi all

i m develop a application in vc++ using ATL/BHO name websapher to block a website from childrens. it work greate but some time it give me error and close the explorer. please help me to make this usefull. my code is:

STDMETHODIMP CBlocker::SetSite(IUnknown* pUnkSite)
{


if (pUnkSite != NULL)
{
// Cache the pointer to IWebBrowser2.
HRESULT hr = pUnkSite->QueryInterface(IID_IWebBrowser2, (void **)&m_spWebBrowser);
if (SUCCEEDED(hr))
{
// Register to sink events from DWebBrowserEvents2.
hr = DispEventAdvise(m_spWebBrowser);
if (SUCCEEDED(hr))
{
m_fAdvised = TRUE;
}
}
}
else
{
// Unregister event sink.
if (m_fAdvised)
{
DispEventUnadvise(m_spWebBrowser);
m_fAdvised = FALSE;
}

// Release cached pointers and other resources here.
m_spWebBrowser.Release();
}

// Call base class implementation.
return IObjectWithSiteImpl<CBlocker>::SetSite(pUnkSite);



}


void STDMETHODCALLTYPE CBlocker::OnDocumentComplete()
{
CComPtr<IDispatch> pDisp;
HRESULT hr = m_spWebBrowser->get_Document(&pDisp);
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
spHTML = pDisp;
if (spHTML) {
CheckDocument();
}

}


STDMETHODIMP CBlocker::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pvarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
USES_CONVERSION;
HWND hwnd;
HRESULT hr = m_spWebBrowser->get_HWND((LONG*) &hwnd);
BSTR bstrUrlName;

try
{

if (dispidMember == DISPID_DOCUMENTCOMPLETE) {

hr = m_spWebBrowser->get_LocationURL(&bstrUrlName);
if(FAILED(hr))
return hr;

LPTSTR psz = new TCHAR[SysStringLen(bstrUrlName)];
lstrcpy(psz, OLE2T(bstrUrlName));

CString s;
SYSTEMTIME sysTime; // Win32 time information
GetSystemTime(&sysTime);
COleDateTime time5(sysTime);

if(StrCmp((LPCTSTR) "about:blank",(LPCTSTR) psz)!=0)
{
if(GetOptions("Screen Shots"))
{
CString fname=WriteWindowToDIB();
s.Format(_T("%s;%s;%s\n"),(LPCTSTR) psz,time5.Format(),fname);
}

if(GetOptions("VisitedSites"))
SaveVisited(s);

CheckDocument();

}

}

/*if(dispidMember == DISPID_BEFORENAVIGATE2)
{

HRESULT hr = m_spWebBrowser->get_LocationURL(&bstrUrlName);
if(FAILED(hr))
return hr;

LPTSTR psz = new TCHAR[SysStringLen(bstrUrlName)];
lstrcpy(psz, OLE2T(bstrUrlName));

if(isBlockSite((LPCTSTR) psz) && strcmp((const char*) psz,_T("about:blank"))!=0)
{

if (SUCCEEDED(hr))
{
MessageBox(hwnd,_T("This site is Blocked"), _T("Error"), MB_OK);
}

VARIANT vFlags = {0},vTargetFrameName = {0};
m_spWebBrowser->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);
m_spWebBrowser->put_Visible(VARIANT_TRUE);
return S_FALSE;
}

return S_OK;
}*/

if(dispidMember == DISPID_NAVIGATECOMPLETE2)
{

HRESULT hr = m_spWebBrowser->get_LocationURL(&bstrUrlName);
if(FAILED(hr))
return hr;

LPTSTR psz = new TCHAR[SysStringLen(bstrUrlName)];
lstrcpy(psz, OLE2T(bstrUrlName));


if(GetOptions("Block Site"))
{

if(isBlockSite((LPCTSTR) psz) && strcmp((const char*) psz,_T("about:blank"))!=0)
{
if (SUCCEEDED(hr))
{
MessageBox(hwnd,_T("This site is Blocked"), _T("Error"), MB_OK);
}

VARIANT vFlags = {0},vTargetFrameName = {0};
m_spWebBrowser->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);
m_spWebBrowser->put_Visible(VARIANT_TRUE);
return S_OK;
}
}



}

}catch(CException){}

return S_FALSE;
}


HRESULT CBlocker::CheckDocument(void)
{
try

{
USES_CONVERSION;
HWND hwnd;
CComPtr<IDispatch> pDisp;
HRESULT hr = m_spWebBrowser->get_Document(&pDisp);
if (FAILED(hr))
return hr;

CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> spHTML;
spHTML = pDisp;


if (spHTML)
{
CComPtr<IHTMLElement> m_pBody;
hr = spHTML->get_body(&m_pBody);
if (FAILED(hr))
return hr;

BSTR bstrHTMLText;
hr = m_pBody->get_outerHTML(&bstrHTMLText);
if (FAILED(hr))
return hr;
LPTSTR psz = new TCHAR[SysStringLen(bstrHTMLText)];
lstrcpy(psz, OLE2T(bstrHTMLText));

if(GetOptions("Block Site"))
{

if(isBlockKey((LPCTSTR) psz) && strcmp((const char*) psz,_T("about:blank"))!=0)
{
hr = m_spWebBrowser->get_HWND((LONG*) &hwnd);

if (SUCCEEDED(hr))
{
MessageBox(hwnd,_T("This site is Blocked"), _T("Error"), MB_OK);
}

VARIANT vFlags = {0},vTargetFrameName = {0};
m_spWebBrowser->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName,NULL,NULL);
m_spWebBrowser->put_Visible(VARIANT_TRUE);
}
}

}
}catch(CException){}
return E_NOTIMPL;
}




Answer this question

block a website with bho give an error

  • Jon Abbott

    Thanks for your reply.

    it give me error when entire page of web site is load and display. some time it never show me error but some time it give me error that close illegal operation sorry for inconvinience. send report, etc.



  • NagaB

    Hi IECustomer

    There are not any other BHO running. im also check with exception handling using try catch block but it give same problem. with same error.

    Thanks



  • Gary Harper

    what is the error you get Do you get the error when you close internet explorer If so, take a look at this discussion http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=782427&SiteID=1
  • createdbyx

    Hi,

    First, during testing make sure that you are not running any other toolbar or BHO addons. Second, test with popup blocking enabled and disabled. These could be reasons for your BHO to raise errors. Yes the exact error message would be helpful. Try placing Messageboxes and error handling in your BHO to exit gracefully when an error occurs.

    Regards.



  • block a website with bho give an error