Connecting to Running Instances of Internet Explorer

There has been always a problem connecting to web browsers, Either you need to write Browser Helper Object or something which browser needs to instantiate. But I found all the other methods had one or other way bug in it. Decided to find simple and bug free way, And came across this ShellWindows interface, which is used by Shell to keep track all the shell browsers including Shell Folders and Internet explorers I Believe shell uses name spaces to keep track of this windows.

Not only shell windows keeps track of all shell windows and also notifies when new shell window is created or deleted.

When you enumerate, you can query whether the entry supports IWebBrowser interface, if it does it could be either WebBrowser or ShellBrowser. Once you get hold of IWebBrowser2 interface, you can either use it or listen to DWebBrowserEvents or through it away.

Before you start cranking in, you should include
#import "mshtml.tlb": // Internet Explorer 5
#import "shdocvw.dl"

Connecting To web browser using shell windows interface

void CIEEnumWindowsDlg::ConnectToShell() 
{
 CoInitialize(NULL);

 if(m_spSHWinds == 0) {
  //
  // Get reference to ShellWindows interface
  //
  if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) 
  == S_OK) {
   // Event Sink 
   //
   LPCONNECTIONPOINTCONTAINER pConnPtCont;

   if ((m_spSHWinds != NULL) && 
   SUCCEEDED(m_spSHWinds->QueryInterface(IID_IConnectionPointContainer,
                                         (LPVOID*)&pConnPtCont))) {
    ASSERT(pConnPtCont != NULL);
    LPCONNECTIONPOINT pConnPt = NULL;
    DWORD dwCookie = 0;

    if (SUCCEEDED(pConnPtCont->FindConnectionPoint(
     __uuidof(SHDocVw::DShellWindowsEvents), &pConnPt)))
    {
     ASSERT(pConnPt != NULL);
     pConnPt->Advise( this->GetIDispatch(FALSE), &dwCookie);
     pConnPt->Release();
    }

    pConnPtCont->Release();
   }
  }
  else {
   AfxMessageBox("Shell Windows interface is not avilable");
  }
 }
}

Downloads

Download source - 15 Kb


Comments

  • Need code in c#

    Posted by sharath1984 on 04/15/2011 06:17am

    Hi, I am having a Web app published in Citrix environment. But when I click on a url it will show a blank IE screen. So can you pls give the above written code in c#.net. Thanks in Advance.

    Reply
  • Apllication crashes when trying to read URL's in a timer??

    Posted by Legacy on 01/24/2004 12:00am

    Originally posted by: Himanshu

    Hi,
    I am having a problem that applcaition is crashing when i try to list all IE url's in a timer for whole day using a timer. The problem is occurring once in a while and it happens randomly at any place or URL. i.e. suppose my applciation is trying to read a URL and at the same instant user closes IE the browser pointer is still there but it is unable to read URL as the window has been closed. I am also using try catch exception blocks to catch the exception thrown but each time it fails to catch the exception raised and shows abnormal program termination. Is there any way i can read the URL's and can avoid this error. Please help I need to fix this problem. Any help or pointers are highly appreciated. Thanks a lot in advance.Looking forward to a help. Please..
    Regards,
    Himanshu

    Reply
  • crashing problem? abnormal program termination

    Posted by Legacy on 01/06/2004 12:00am

    Originally posted by: Himanshu

    Hi all,

    I am having a problem when i use this to read url's from IE..It terminates itself and shows an abnormal program termination error..I need to sort this problem out..Can someone help me out in doing the same..i need this to be done..Please help me out..any help or poinetrs are thoroughly appreciated...Thanks a lot in advance..
    Regards,
    Himanshu

    Reply
  • DocumentComplete() / NavigateComplete2() technique

    Posted by Legacy on 11/01/2003 12:00am

    Originally posted by: TheFlashback

    A lot of people have complained that DocumentComplete() / NavigateComplete2() do not get fired 
    
    or get fired too early... Here is the technique that seems to be working properly on XP with IE6:

    // Global...
    LPDISPATCH glpDispatch = NULL;


    BEGIN_DISPATCH_MAP(CIEEnumWindowsDlg, CCmdTarget)
    DISP_FUNCTION_ID(CIEEnumWindowsDlg, "NavigateComplete2",0x000000fc,NavigateComplete2,VT_EMPTY,VTS_DISPATCH VTS_VARIANT)
    DISP_FUNCTION_ID(CIEEnumWindowsDlg, "DocumentComplete",0x00000103,DocumentComplete,VT_EMPTY,VTS_DISPATCH VTS_VARIANT)
    END_DISPATCH_MAP()

    void CIEEnumWindowsDlg::NavigateComplete2(LPDISPATCH lpDispatch,
    VARIANT* pURL)
    {
    if (glpDispatch == 0)
    glpDispatch = lpDispatch;
    }


    void CIEEnumWindowsDlg::DocumentComplete(LPDISPATCH lpDispatch,
    VARIANT* pURL)
    {
    if (glpDispatch == 0 || glpDispatch != lpDispatch)
    return;

    CString strOldText;
    m_Info.GetWindowText(strOldText);
    strOldText += _T("\r\nDocument is done downloading");
    m_Info.SetWindowText(strOldText);

    glpDispatch = NULL;

    IWebBrowser2* pBrowser = 0;
    if (SUCCEEDED(lpDispatch->QueryInterface(IID_IWebBrowser2, (LPVOID*)&pBrowser)))
    {
    BSTR bStrURL;
    pBrowser->get_LocationURL(&bStrURL);

    m_Info.GetWindowText(strOldText);
    strOldText += _T(" URL: ");
    strOldText += CString(bStrURL);
    m_Info.SetWindowText(strOldText);

    SysFreeString(bStrURL);
    }
    }

    Reply
  • URGENT - Calling IE programmatically and Hiding display

    Posted by Legacy on 10/30/2003 12:00am

    Originally posted by: Varun

    Hi All,
    
    

    I need to write a program in JAVA which will call IE and open and HTML page in it. But I want that the display of this IE window should not appear before user. Best will be if there will be no sign of opening of IE before user. Second best option is IE appears as a button on taskbar but user is not able to access (restore it to view) it.
    is it possible?

    Pls reply asap.

    Varun

    Reply
  • How can I set the proxy to use by connected running instance

    Posted by Legacy on 08/14/2003 12:00am

    Originally posted by: agsapt

    I would like to know if it's possible that once we are connected to a running instance, we can set a proxy to use by that running instance? If that's possible then how?

    Reply
  • how to fire BeforeNavigate2 event???

    Posted by Legacy on 08/05/2003 12:00am

    Originally posted by: rain

    I want to get a notification when user input a url in address bar or click a link, I think this can be done by capture BeforeNavigate2 event, but I failed to get the notification of this event, what can I do to get the notification? Thanks!

    Reply
  • Need help -> FileDownload eventhandler not firing

    Posted by Legacy on 07/18/2003 12:00am

    Originally posted by: Dickson

    After connecting to an instance of IE, i'm trying to control/monitor the events. So far everything works but the filedownload event handler. Anybody have any tips on how to do it? The following (C#) is the syntax for all of my event handlers.

    myInternetExplorer.FileDownload += new SHDocVw.DWebBrowserEvents2_FileDownloadEventHandler(OnDownload);

    Any clues/pointers would be helpful! I want to build my own download manager :)

    Thanks,
    Dickson

    Reply
  • how to set URL

    Posted by Legacy on 07/08/2003 12:00am

    Originally posted by: tony

    anyone know how can I use a simple wrapper class/application such at I can from my application tell the instant to visit the url one by one? I don't need to get anything back, just need to visit the links and make sure the links are all working.

    thanks

    Reply
  • Possibility to use Navigate ?

    Posted by Legacy on 05/26/2003 12:00am

    Originally posted by: Philippe Legrand

    Hi,
    
    

    Glad to have poken around and found this article !!
    BUT :( I am unable to use the .Navigate ou Navigate2 methods on IWebBrowser2 interface :((

    Here is my piece of code, plugged into the sample source code :
    Everything is working smoothly except MY CODE : the navigate call ! :((

    Is there any limitation on the interfaces we got by this way ?? Any idea why Navigate does return -858993460 and leads my program to "unknow error" ??

    Thanks for your help and sorry for code indenting :(

    Philippe.

    void CIEEnumWindowsDlg::OnVs()
    {
    // TODO: Add your control notification handler code here
    _bstr_t locationtogo="http://help.yahoo.com/help/us/edit/edit-01.html";;
    VARIANT vURL, vDummy;
    vURL.vt = VT_BSTR;
    vURL.bstrVal = locationtogo.copy();

    int index = m_List.GetCurSel();
    if(index != CB_ERR) {
    IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_List.GetItemDataPtr(index);
    if(pBrowser) {
    AfxMessageBox("Navigate now");
    //int a=pBrowser->Navigate(locationtogo,NULL,NULL,NULL,NULL);
    int a=pBrowser->Navigate(locationtogo, &vDummy, &vDummy, &vDummy, &vDummy);
    if (a!=S_OK) {
    if (a==E_INVALIDARG) AfxMessageBox("Nav failed due to bad args");
    else if (a==E_OUTOFMEMORY) AfxMessageBox("Nav failed out of memory !");
    else AfxMessageBox("unknown error");
    }
    IDispatchPtr spDisp;
    if(pBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 ) {
    MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
    MSHTML::IHTMLElementPtr spHtmlElement;
    spHtmlDocument->get_body(&spHtmlElement);
    if(spHtmlElement != 0) {
    BSTR bstr;
    spHtmlElement->get_outerHTML(&bstr);
    m_Info.SetWindowText(CString(bstr));
    }
    }
    }

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds