BHO: Navigate to a different URL in BeforeNavigate2 event

I have developed a simple add-on for IE using the example available at:
http://msdn.microsoft.com/en-gb/library/bb250489(VS.85).aspx 

I am using BeforeNavigate2 event instead of DocumentComplete Event in the example.

Here, I read the URL that is trying to get opened. If the domain is "www.google.com", I am trying to change the URL to "www.experts-exchange.com". I am not able to find out a way of changing the existing URL and open it. I have attached a few lines of my code. Kindly have a look and guide me on how to proceed.

ps: I am a C++ programmer and ATL/COM is completely new to me. So please give your suggestions with sample code.
BEGIN_SINK_MAP(CIEPluginBHO)
    SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, OnBeforeNavigate2)
END_SINK_MAP()
 
    // DWebBrowserEvents2
  void STDMETHODCALLTYPE OnBeforeNavigate2(IDispatch* pDisp, VARIANT* URL, 
  VARIANT* pvtFlags,
  VARIANT* pvtTargetFrameName, 
  VARIANT* pvtPostData, 
  VARIANT* pvtHeaders,
  VARIANT_BOOL* pvbCancel);
 
 
/// In my cpp file
 
void STDMETHODCALLTYPE CIEPluginBHO::OnBeforeNavigate2(IDispatch* pDisp,
  VARIANT* URL,
  VARIANT* Flags,
  VARIANT* TargetFrameName, 
  VARIANT* PostData, 
  VARIANT* Headers,
  VARIANT_BOOL* Cancel)
{
	static TCHAR searchText[] = _T("www.google.com");
	CString currentURL(URL->bstrVal);
	if (currentURL.Find(searchText) != -1)
	{
				
	}
}

Open in new window

Cool-GuyAsked:
Who is Participating?
 
alb66Commented:
it should be:

if (currentURL.Find(searchText) != -1)
{
    // Query for the IWebBrowser2 interface.
    CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
    spTempWebBrowser .Navigate2( "www....
0
 
Cool-GuyAuthor Commented:
I added the following:
if (currentURL.Find(searchText) == -1)
{
        // Query for the IWebBrowser2 interface.
       CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
      CComVariant vURL(L"http://www.google.com");

      spTempWebBrowser->Navigate2(&vURL, NULL,NULL,NULL,NULL);            
}

IE fails to open when the plugin registers for the above code. When I comment out "spTempWebBrowser->Navigate2" statement, IE opens i.e.
if (currentURL.Find(searchText) == -1)
{
        // Query for the IWebBrowser2 interface.
       CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
      CComVariant vURL(L"http:/www.experts-exchange.com");

//     spTempWebBrowser->Navigate2(&vURL, NULL,NULL,NULL,NULL);            
}
0
 
Cool-GuyAuthor Commented:
This worked:
if (currentURL.Find(searchText) != -1)
	{
		// Query for the IWebBrowser2 interface.
		CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
		CComVariant vURL("http://www.experts-exchange.com");
		CComVariant vNull;	
		vNull.vt = VT_NULL;
		spTempWebBrowser->Stop();
		spTempWebBrowser->Navigate2(&vURL, &vNull, &vNull, &vNull, &vNull);		
	}

Open in new window

0
 
Cool-GuyAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.