?
Solved

BHO: Navigate to a different URL in BeforeNavigate2 event

Posted on 2009-02-17
4
Medium Priority
?
6,763 Views
Last Modified: 2013-12-03
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

0
Comment
Question by:Cool-Guy
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
alb66 earned 1500 total points
ID: 23667746
it should be:

if (currentURL.Find(searchText) != -1)
{
    // Query for the IWebBrowser2 interface.
    CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;
    spTempWebBrowser .Navigate2( "www....
0
 

Author Comment

by:Cool-Guy
ID: 23718729
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
 

Author Comment

by:Cool-Guy
ID: 23771773
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
 

Author Closing Comment

by:Cool-Guy
ID: 31547733
Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
I made this because I wanted to get e-mail with a attached csv file so I'd would be able to import user input into a MS Excel template, but I also wanted to register/save all inputs from each day in a file on the server. 1st - It creates a temp C…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question