Changing Internet Explorer URL Dynamically using C++

override_00
override_00 used Ask the Experts™
on
Hi,

I wanna to find out how exactly can i detect an URL in internet explorer and dynamically change the URL when the URL does not contain the required information?

Will appreciate if you can provides source code.


Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
heres how to detect the URL

IWebBrowser2 * pBrowser;
LPUNKNOWN pUnk;

hr = ::GetActiveObject(CLSID_InternetExplorer, NULL, &pUnk);
if (SUCCEEDED(hr))
{
     hr = pUnk->QueryInterface(IID_IWebBrowser2, (LPVOID*)&pBrowser);
     pUnk->Release();

        BSTR bsz;
     pBroswer->get_LocationURL(&bsz);
}

changing the URL however is not directly possible because there is no such method as put_LocationURL. You can however sink the events and change the url in OnBeforeNavigate2.

Author

Commented:
Thank You... I have successfully been able to detect URL ... but how do you actually sink the events or change the URL?
spcifically you have to write a COM object that implements DIID_DWebBrowserEvents2, and then use IConnectionPoint interface to sink the events.


class CSome :
     public CComObjectRoot,
     public IDispatchImpl<DWebBrowserEvents2, &DIID_DWebBrowserEvents2, &LIBID_TSomeib>
{
..
BEGIN_COM_MAP(CTierFileNode)
     COM_INTERFACE_ENTRY(_IEvents)
END_COM_MAP()

.... // Hanlders for events

STDMETHOD(OnBeforeNavigate2)(LPDISPATCH  pDisp , VARIANT* URL, VARIANT* Flags, VARIANT* TargetFrameName,VARIANT* PostData, VARIANT* Headers, BOOL* Cancel);

etc.

}

CComObject<CSome>* pSome;
CComObject<CSome>::CreateInstance(&pSome);

CComPtr<IUnknown> pUnk;
pSome->QueryInterface(IID_IUnknown, (void**)&pUnk);
AtlAdvise(m_pWebBrowser, pUnk, DIID_DWebBrowserEvents2, &dwCookie);

Commented:
Seems like for IE 8 that way already does not work. Probably, Browser Helper Objects may help:
http://msdn.microsoft.com/en-us/library/bb250436(VS.85).aspx
 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial