TCHAR strLoader[MAX_PATH]; ::GetModuleFileName (NULL, strLoader, MAX_PATH); if(stricmp("explorer.exe", strLoader) == 0) return FALSE; |
STDMETHODIMP CBhoApp::SetSite(IUnknown *pUnkSite) { //获取并存储IWebBrowser2指针 m_spWebBrowser2 = pUnkSite; if (m_spWebBrowser2 == NULL) return E_INVALIDARG; //获取并存储IConnectionPointerContainer指针 m_spCPC = m_spWebBrowser2; if (m_spCPC == NULL) return E_POINTER; //连接到宿主程序以接收事件通知 return Connect(); } |
HRESULT CBhoApp::Connect() { HRESULT hr; CComPtr<IConnectionPoint> spCP; //获取访问WebBrowser事件的连接指针 hr = m_spCPC->FindConnectionPoint(DIID_DWebBrowserEvents2, &spCP); if (FAILED(hr)) return hr; //把事件处理程序传递给宿主程序Each time an event //每次有事件产生时,宿主程序都会调用我们实现的IDispatch接口的函数 hr = spCP->Advise(reinterpret_cast<IDispatch*>(this),&m_dwCookie); return hr; } |
通过调用Advise方法,告之浏览器BHO想要接受事件通知,这意味着BHO会把指向IDispatch的指针提供给浏览器(这是由于要进行组件事件处理),接下来,浏览器会调用IDispatch的Invoke方法,并传递给它一个事件的ID作为参数,因此,BHO必须实现Invoke方法以处理所发生的事件。
STDMETHODIMP CBhoApp::Invoke(DISPID dispidMember,REFIID riid, LCID lcid,WORD wFlags, DISPPARAMS *pDispParams,VARIANT *pvarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) { //在使用ATL字符串转换宏(此处用的是OLE2T)防止编译错误时,必须先调用这个宏 USES_CONVERSION; if(dispidMember == DISPID_BEFORENAVIGATE2) { BSTR bstrUrlName; HRESULT hr = m_spWebBrowser2->get_LocationURL(&bstrUrlName); if(FAILED(hr)) return hr; LPTSTR psz = new TCHAR[SysStringLen(bstrUrlName)]; lstrcpy(psz, OLE2T(bstrUrlName)); //此处,直接比较www.xyz.com,也可从一个要屏蔽的网站列表中进行比较。 if(stricmp("http://www.xyz.com/",psz) == 0) { VARIANT vFlags = {0},vTargetFrameName = {0}; //如果不想显示"空白页",也可重定向至某个表明此网站已被屏蔽的页面。 m_spWebBrowser2->Navigate(SysAllocString(L"about:blank"),&vFlags,&vTargetFrameName, NULL,NULL); m_spWebBrowser2->put_Visible(VARIANT_TRUE); return S_FALSE; } return S_OK; } else if(dispidMember == DISPID_NAVIGATECOMPLETE2) //检查以防止页面的post导航加载(post-navigation loading) { BSTR bstrUrlName; HRESULT hr = m_spWebBrowser2->get_LocationURL(&bstrUrlName); if(FAILED(hr)) return hr; //把文本从Unicode转换为ANSI LPTSTR psz = new TCHAR[SysStringLen(bstrUrlName)]; lstrcpy(psz, OLE2T(bstrUrlName)); ::OutputDebugString("In Navigate Complete"); ::OutputDebugString(psz); if(stricmp("http://www.xyz.com/",psz) == 0) { VARIANT vFlags = {0},vTargetFrameName = {0}; m_spWebBrowser2->Navigate(SysAllocString(L"about:blank"), &vFlags,&vTargetFrameName, NULL,NULL); m_spWebBrowser2->put_Visible(VARIANT_TRUE); } return S_OK; } return S_FALSE; } |
HKLM SOFTWAREMicrosoftWindowsCurrentVersionExplorerBrowser Helper Objects ForceRemove {0CB66BA8-5E1F-4963-93D1-E1D6B78FE9A2} |