Click here to Skip to main content
15,898,985 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi all.
In my BHO I'd like to redirect some URL's before the page is loaded.
I've read the documentation about IExplorer Dispatch Events in MSDN, and found that this better to do before navigate
So I made a function connected to DISPID_BEFORENAVIGATE2:

<br />
void MyBhoClass::OnBeforeNavigate( IDispatch *pDisp, VARIANT *url, VARIANT *Flags, VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers, VARIANT_BOOL *Cancel )<br />
{<br />
	//<br />
	std::string urltmp;<br />
	wchar_to_char( url->bstrVal, urltmp );<br />
	HRESULT hr;<br />
	VARIANT vr;<br />
	if( std::string::npos != urltmp.find( "xxx" ) ) // for example<br />
	{<br />
		vr.vt = VT_BSTR;<br />
		vr.bstrVal = SysAllocString( L"http://some.other.url.com" );<br />
		hr = m_browser->Navigate2( &vr, 0, 0, 0, 0 );<br />
	}<br />
}<br />


This code works well!

but in this part there is a problem:
<br />
<br />
void MyBhoClass::OnBeforeNavigate( IDispatch *pDisp, VARIANT *url, VARIANT *Flags, VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers, VARIANT_BOOL *Cancel )<br />
{<br />
	//<br />
	std::string urltmp;<br />
	wchar_to_char( url->bstrVal, urltmp );<br />
	HRESULT hr;<br />
	VARIANT vr;<br />
	if( 0 == urltmp.find( "about:" ) ) // i.e. the new tab is open<br />
	{<br />
		vr.vt = VT_BSTR;<br />
		vr.bstrVal = SysAllocString( L"http://some.other.url.com" );<br />
 		MessageBox( GetForegroundWindow(), "Before Navigate!!!!", "Debug_before_navigate", MB_OK | MB_ICONEXCLAMATION );<br />
		hr = m_browser->Navigate2( &vr, 0, 0, 0, 0 );<br />
	}<br />
}<br />


when a new tab is open MessageBox is invoked. After calling the function Navigate2() we come one more time to OnBeforeNavigate() function, and here the variable "url" has the proper value (i.e "http://some.other.url.com"). Therefore OnBeforeNavigate() do "nothing" at this time. But the the browser opens a blank page.

And one more thing... during the debugging I c, that pressing F10 on Navigate2() function bring me to the beginning of function OnBeforeNavigate().

How can I do redirect when the new tab is open?
Thanks!

[SOLVED with workround]
Redirect gives the wanted result in DISPID_NAVIGATECOMPLETE2 handler. however... I can't understand why it don't work in DISPID_BEFORENAVIGATE2 :(
Posted
Updated 2-Feb-10 23:37pm
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900