Partager via


Binding to Internet Explorer Instances

A customer wanted to run code whenever a user started Internet Explorer.

A possible solution is to use a timer to check the Running Object Table periodically:

ox=CREATEOBJECT("monitor")

ox.show

DEFINE CLASS monitor as Form

      AllowOutput=.f.

      left=300

      ADD OBJECT tmr as timer WITH interval=2000

      PROCEDURE tmr.timer

            TRY

* obj=GETOBJECT(,"excel.application")

                  obj=GETOBJECT(,"InternetExplorer.application")

                  ?PROGRAM(),obj

            CATCH

                  ?"not avail"

            ENDTRY

                       

ENDDEFINE

However, this approach works for Excel, but doesn’t work for Internet Explorer. See How to connect to a running instance of Internet Explorer

Another approach is to bind events to the ShellWindows object. The code below will respond to new instances of IE being started, Windows Explorer instances navigating to HTTP addresses, and navigations from within existing IE instances.

See also

What's the difference between Internet Explorer and Explorer?

Binding to Internet Explorer events: bug in Typelibrary

PRB: Microsoft Excel Not Registered in Running Object Table

 

CLEAR ALL

CLEAR

PUBLIC oShellWindowsEventsHandler,oShell,nIECnt,oWebBrowserEventsHandler

oShell=CREATEOBJECT("shell.application")

oWebBrowserEventsHandler = CREATEOBJECT("WebBrowserEventsHandler")

oShellWindowsEventsHandler=create("ShellWindowsEventsHandler")

?EVENTHANDLER(oShell.windows(),oShellWindowsEventsHandler)

RegisterIE()

PROCEDURE RegisterIE

      ?oShell.windows().count

      IF oShell.windows().count<1

            RETURN

      ENDIF

      PUBLIC oie[oShell.windows().count]

      oie=0

      nIECnt=0

      FOR EACH x IN oShell.windows

            nIECnt=nIECnt+1

            oie[nIECnt]=GETINTERFACE(x,"IWebBrowser2","shdocvw.dll")

            ?"EVH",EVENTHANDLER(oie[nIECnt],oWebBrowserEventsHandler)

            fIsIE=.f.

            TRY

                  odoc=oie[nIECnt].Document()

                  od = GETINTERFACE(odoc,"IHTMLDocument2","mshtml.tlb")

                  ?"IE Window "+od.Title

                  ??oie[nIECnt].locationurl,SPACE(10),oie[nIECnt].locationname

            CATCH TO oExcept

                  ?oExcept.Message,oExcept.Details,oExcept.linecontents

            ENDTRY

      ENDFOR

      RETURN

DEFINE CLASS ShellWindowsEventsHandler AS session

      IMPLEMENTS DShellWindowsEvents IN "c:\windows\system32\shdocvw.dll"

      PROCEDURE DShellWindowsEvents_WindowRegistered(lCookie AS Number) AS VOID;

  HELPSTRING "A new window was registered."

  ?PROGRAM(),lCookie

            RELEASE oie

  RegisterIE()

      PROCEDURE DShellWindowsEvents_WindowRevoked(lCookie AS Number) AS VOID;

  HELPSTRING "A new window was revoked."

  ?PROGRAM(),lCookie

ENDDEFINE

DEFINE CLASS WebBrowserEventsHandler AS session

      m_ie=0

      IMPLEMENTS DWebBrowserEvents2 IN "c:\windows\system32\shdocvw.dll"

      PROCEDURE DWebBrowserEvents2_StatusTextChange(Text AS STRING) AS VOID;

  HELPSTRING "Statusbar text changed."

      PROCEDURE DWebBrowserEvents2_ProgressChange(Progress AS Number, ProgressMax AS Number) AS VOID;

  HELPSTRING "Fired when download progress is updated."

      PROCEDURE DWebBrowserEvents2_CommandStateChange(Command AS Number, Enable AS LOGICAL) AS VOID;

  HELPSTRING "The enabled state of a command changed."

      PROCEDURE DWebBrowserEvents2_DownloadBegin() AS VOID;

  HELPSTRING "Download of a page started."

      PROCEDURE DWebBrowserEvents2_DownloadComplete() AS VOID;

  HELPSTRING "Download of page complete."

      PROCEDURE DWebBrowserEvents2_TitleChange(Text AS STRING) AS VOID;

  HELPSTRING "Document title changed."

      PROCEDURE DWebBrowserEvents2_PropertyChange(szProperty AS STRING) AS VOID;

  HELPSTRING "Fired when the PutProperty method has been called."

      PROCEDURE DWebBrowserEvents2_BeforeNavigate2(pDisp AS VARIANT, URL AS VARIANT, Flags AS VARIANT,;

                  TargetFrameName AS VARIANT, PostData AS VARIANT, Headers AS VARIANT, Cancel AS LOGICAL @) AS VOID;

  HELPSTRING "Fired before navigate occurs in the given WebBrowser (window or frameset element). The processing of this navigation may be modified."

  ?PROGRAM(), URL

      PROCEDURE DWebBrowserEvents2_NewWindow2(ppDisp AS VARIANT @, Cancel AS LOGICAL @) AS VOID;

  HELPSTRING "A new, hidden, non-navigated WebBrowser window is needed."

      PROCEDURE DWebBrowserEvents2_NavigateComplete2(pDisp AS VARIANT, URL AS VARIANT) AS VOID;

  HELPSTRING "Fired when the document being navigated to becomes visible and enters the navigation stack."

      PROCEDURE DWebBrowserEvents2_DocumentComplete(pDisp AS VARIANT, URL AS VARIANT) AS VOID;

  HELPSTRING "Fired when the document being navigated to reaches ReadyState_Complete."

      PROCEDURE DWebBrowserEvents2_OnQuit() AS VOID;

  HELPSTRING "Fired when application is quiting."

  ?PROGRAM()

      PROCEDURE DWebBrowserEvents2_OnVisible(Visible AS LOGICAL) AS VOID;

  HELPSTRING "Fired when the window should be shown/hidden"

      PROCEDURE DWebBrowserEvents2_OnToolBar(ToolBar AS LOGICAL) AS VOID;

  HELPSTRING "Fired when the toolbar should be shown/hidden"

      PROCEDURE DWebBrowserEvents2_OnMenuBar(MenuBar AS LOGICAL) AS VOID;

  HELPSTRING "Fired when the menubar should be shown/hidden"

      PROCEDURE DWebBrowserEvents2_OnStatusBar(StatusBar AS LOGICAL) AS VOID;

  HELPSTRING "Fired when the statusbar should be shown/hidden"

      PROCEDURE DWebBrowserEvents2_OnFullScreen(FullScreen AS LOGICAL) AS VOID;

  HELPSTRING "Fired when fullscreen mode should be on/off"

      PROCEDURE DWebBrowserEvents2_OnTheaterMode(TheaterMode AS LOGICAL) AS VOID;

  HELPSTRING "Fired when theater mode should be on/off"

      PROCEDURE DWebBrowserEvents2_WindowSetResizable(Resizable AS LOGICAL) AS VOID;

  HELPSTRING "Fired when the host window should allow/disallow resizing"

      PROCEDURE DWebBrowserEvents2_WindowSetLeft(Left AS Number) AS VOID;

  HELPSTRING "Fired when the host window should change its Left coordinate"

      PROCEDURE DWebBrowserEvents2_WindowSetTop(Top AS Number) AS VOID;

  HELPSTRING "Fired when the host window should change its Top coordinate"

      PROCEDURE DWebBrowserEvents2_WindowSetWidth(Width AS Number) AS VOID;

  HELPSTRING "Fired when the host window should change its width"

      PROCEDURE DWebBrowserEvents2_WindowSetHeight(Height AS Number) AS VOID;

  HELPSTRING "Fired when the host window should change its height"

      PROCEDURE DWebBrowserEvents2_WindowClosing(IsChildWindow AS LOGICAL, Cancel AS LOGICAL @) AS VOID;

  HELPSTRING "Fired when the WebBrowser is about to be closed by script"

      PROCEDURE DWebBrowserEvents2_ClientToHostWindow(CX AS Number @, CY AS Number @) AS VOID;

  HELPSTRING "Fired to request client sizes be converted to host window sizes"

      PROCEDURE DWebBrowserEvents2_SetSecureLockIcon(SecureLockIcon AS Number) AS VOID;

  HELPSTRING "Fired to indicate the security level of the current web page contents"

* PROCEDURE DWebBrowserEvents2_FileDownload(fActiveDoc as Logical @, Cancel AS LOGICAL @) AS VOID;

  HELPSTRING "Fired to indicate the File Download dialog is opening"

      PROCEDURE DWebBrowserEvents2_FileDownload(Cancel AS LOGICAL @) AS VOID;

  HELPSTRING "Fired to indicate the File Download dialog is opening"

      PROCEDURE DWebBrowserEvents2_NavigateError(pDisp AS VARIANT, URL AS VARIANT, Frame AS VARIANT, StatusCode AS VARIANT, Cancel AS LOGICAL @) AS VOID;

  HELPSTRING "Fired when a binding error occurs (window or frameset element)."

      PROCEDURE DWebBrowserEvents2_PrintTemplateInstantiation(pDisp AS VARIANT) AS VOID;

  HELPSTRING "Fired when a print template is instantiated."

      PROCEDURE DWebBrowserEvents2_PrintTemplateTeardown(pDisp AS VARIANT) AS VOID;

  HELPSTRING "Fired when a print template destroyed."

      PROCEDURE DWebBrowserEvents2_UpdatePageStatus(pDisp AS VARIANT, nPage AS VARIANT, fDone AS VARIANT) AS VOID;

  HELPSTRING "Fired when a page is spooled. When it is fired can be changed by a custom template."

      PROCEDURE DWebBrowserEvents2_PrivacyImpactedStateChange(bImpacted AS LOGICAL) AS VOID;

  HELPSTRING "Fired when the global privacy impacted state changes"

ENDDEFINE

Comments

  • Anonymous
    September 07, 2005
    Wow !

    Thanks a million Calvin, this is a GREAT example of what can be done with bind events.

    Benjamin Siegel
  • Anonymous
    September 07, 2005
    Hi Calvin,

    First I just want to say I love the info on your blog. :)

    For my comment...I tried the code but i get an error that says "DWebBrowserEvents2_NewWindow3" is not found.

    So I pulled up the method in the Object Browser and added it to the code. It happens to have more parameters than "NewWindow2"

    ...but now I get an error that says there are not enough parameters for "DWebBrowserEvents2_NewWindow3". I wonder also why when dragging the interface from the Object Browser to a prg the "NewWindow3" method does not get copied over...


  • Anonymous
    September 07, 2005
    I get the same results as Alan....
  • Anonymous
    September 07, 2005
    Try this...

    CLEAR ALL
    CLEAR


    ON ERROR myError()

    PUBLIC goShellWindowsEventsHandler, goWebBrowserEventsHandler, ;
    goShell AS SHELL.APPLICATION, gnIECnt AS INTEGER

    goShell = CREATEOBJECT( 'shell.application' )

    goWebBrowserEventsHandler = NEWOBJECT( 'WebBrowserEventsHandler' )

    goShellWindowsEventsHandler = NEWOBJECT( 'ShellWindowsEventsHandler' )

    EVENTHANDLER( goShell.WINDOWS(), goShellWindowsEventsHandler )

    RegisterIE( )



    PROCEDURE myError
    * ? "ERROR FROM ROUTINE"
    ? ""
    RETURN


    -------------------------------------------------------
    PROCEDURE RegisterIE
    LOCAL lnShellWinCnt AS INTEGER
    lnShellWinCnt = goShell.WINDOWS().COUNT

    ? 'lnShellWinCnt: ', lnShellWinCnt

    IF lnShellWinCnt < 1
    RETURN
    ENDIF

    PUBLIC gaShellWins[lnShellWinCnt]
    LOCAL lnIECnt AS INTEGER, loObj AS OBJECT

    lnIECnt = 0
    gaShellWins = 0


    CLEAR


    FOR EACH loObj IN goShell.WINDOWS

    lnIECnt = lnIECnt + 1
    gaShellWins[lnIECnt] = GETINTERFACE( loObj, 'IWebBrowser2', 'shdocvw.dll' )
    EVENTHANDLER( gaShellWins[lnIECnt], goWebBrowserEventsHandler )
    fIsIE = .F.

    TRY
    odoc = gaShellWins[lnIECnt].DOCUMENT()
    od = GETINTERFACE( odoc, 'IHTMLDocument2', 'mshtml.tlb' )
    ! ? '==IE WINDOW ' + TRANSFORM( lnIECnt ) + '=='
    ! ? ' TITLE: ', od.TITLE
    ! ? ' LocationURL: ', gaShellWins[lnIECnt].locationurl
    ! ? ' LocationName: ', gaShellWins[lnIECnt].locationname
    ! ?
    CATCH TO oExcept
    -- May get this error if we find an IE Window that is not
    -- a browser window, such as a Windows Explorer window.

    !
    ? "ERROR FROM TRY CATCH"
    !
    ? oExcept.MESSAGE
    !
    ? oExcept.DETAILS
    ! ? oExcept.LINECONTENTS
    ! ?
    ENDTRY



    ENDFOR
    ENDPROC


    ------------------------------------------------------------------

    HANDLER CLASSES

    ------------------------------------------------------------------
    DEFINE CLASS ShellWindowsEventsHandler AS SESSION
    IMPLEMENTS DShellWindowsEvents IN SHDOCVW.DLL

    PROCEDURE DShellWindowsEvents_WindowRegistered( lCookie AS NUMBER ) AS VOID
    ? PROGRAM(), lCookie
    RELEASE gaShellWins
    RegisterIE( )
    ENDPROC

    PROCEDURE DShellWindowsEvents_WindowRevoked( lCookie AS NUMBER ) AS VOID
    ? PROGRAM( ), lCookie
    ENDPROC

    ENDDEFINE


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    DEFINE CLASS WebBrowserEventsHandler AS custom

    IMPLEMENTS DWebBrowserEvents2 IN SHDOCVW.DLL

    PROCEDURE logit(cstr as String)
    * ? cstr
    ENDPROC


    PROCEDURE init
    * this.logit(PROGRAM())
    ENDPROC





    PROCEDURE DWebBrowserEvents2_StatusTextChange(Text AS STRING) AS VOID;
    HELPSTRING "Statusbar text changed."
    * add user code here
    this.logit(PROGRAM())

    ENDPROC

    PROCEDURE DWebBrowserEvents2_ProgressChange(Progress AS Number, ProgressMax AS Number) AS VOID;
    HELPSTRING "Fired when download progress is updated."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_CommandStateChange(Command AS Number, Enable AS LOGICAL) AS VOID;
    HELPSTRING "The enabled state of a command changed."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_DownloadBegin() AS VOID;
    HELPSTRING "Download of a page started."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_DownloadComplete() AS VOID;
    HELPSTRING "Download of page complete."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_TitleChange(Text AS STRING) AS VOID;
    HELPSTRING "Document title changed."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_PropertyChange(szProperty AS STRING) AS VOID;
    HELPSTRING "Fired when the PutProperty method has been called."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC





    PROCEDURE DWebBrowserEvents2_BeforeNavigate2(pDisp AS VARIANT, URL AS VARIANT, Flags AS VARIANT, TargetFrameName AS VARIANT, PostData AS VARIANT, Headers AS VARIANT, Cancel AS LOGICAL @) AS VOID;
    HELPSTRING "Fired before navigate occurs in the given WebBrowser (window or frameset element). The processing of this navigation may be modified."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC






    PROCEDURE DWebBrowserEvents2_NewWindow2(ppDisp AS VARIANT @, Cancel AS LOGICAL @) AS VOID;
    HELPSTRING "A new, hidden, non-navigated WebBrowser window is needed."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_NavigateComplete2(pDisp AS VARIANT, URL AS VARIANT) AS VOID;
    HELPSTRING "Fired when the document being navigated to becomes visible and enters the navigation stack."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_DocumentComplete(pDisp AS VARIANT, URL AS VARIANT) AS VOID;
    HELPSTRING "Fired when the document being navigated to reaches ReadyState_Complete."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_OnQuit() AS VOID;
    HELPSTRING "Fired when application is quiting."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_OnVisible(Visible AS LOGICAL) AS VOID;
    HELPSTRING "Fired when the window should be shown/hidden"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_OnToolBar(ToolBar AS LOGICAL) AS VOID;
    HELPSTRING "Fired when the toolbar should be shown/hidden"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_OnMenuBar(MenuBar AS LOGICAL) AS VOID;
    HELPSTRING "Fired when the menubar should be shown/hidden"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_OnStatusBar(StatusBar AS LOGICAL) AS VOID;
    HELPSTRING "Fired when the statusbar should be shown/hidden"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_OnFullScreen(FullScreen AS LOGICAL) AS VOID;
    HELPSTRING "Fired when fullscreen mode should be on/off"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_OnTheaterMode(TheaterMode AS LOGICAL) AS VOID;
    HELPSTRING "Fired when theater mode should be on/off"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_WindowSetResizable(Resizable AS LOGICAL) AS VOID;
    HELPSTRING "Fired when the host window should allow/disallow resizing"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_WindowSetLeft(Left AS Number) AS VOID;
    HELPSTRING "Fired when the host window should change its Left coordinate"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_WindowSetTop(Top AS Number) AS VOID;
    HELPSTRING "Fired when the host window should change its Top coordinate"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_WindowSetWidth(Width AS Number) AS VOID;
    HELPSTRING "Fired when the host window should change its width"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_WindowSetHeight(Height AS Number) AS VOID;
    HELPSTRING "Fired when the host window should change its height"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_WindowClosing(IsChildWindow AS LOGICAL, Cancel AS LOGICAL @) AS VOID;
    HELPSTRING "Fired when the WebBrowser is about to be closed by script"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_ClientToHostWindow(CX AS Number @, CY AS Number @) AS VOID;
    HELPSTRING "Fired to request client sizes be converted to host window sizes"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_SetSecureLockIcon(SecureLockIcon AS Number) AS VOID;
    HELPSTRING "Fired to indicate the security level of the current web page contents"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_FileDownload(Cancel AS LOGICAL @) AS VOID;
    HELPSTRING "Fired to indicate the File Download dialog is opening"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_NavigateError(pDisp AS VARIANT, URL AS VARIANT, Frame AS VARIANT, StatusCode AS VARIANT, Cancel AS LOGICAL @) AS VOID;
    HELPSTRING "Fired when a binding error occurs (window or frameset element)."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_PrintTemplateInstantiation(pDisp AS VARIANT) AS VOID;
    HELPSTRING "Fired when a print template is instantiated."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_PrintTemplateTeardown(pDisp AS VARIANT) AS VOID;
    HELPSTRING "Fired when a print template destroyed."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_UpdatePageStatus(pDisp AS VARIANT, nPage AS VARIANT, fDone AS VARIANT) AS VOID;
    HELPSTRING "Fired when a page is spooled. When it is fired can be changed by a custom template."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_PrivacyImpactedStateChange(bImpacted AS LOGICAL) AS VOID;
    HELPSTRING "Fired when the global privacy impacted state changes"
    * add user code here
    this.logit(PROGRAM())
    ENDPROC

    PROCEDURE DWebBrowserEvents2_NewWindow3(ppDisp AS VARIANT @, Cancel AS LOGICAL @, dwFlags AS NUMBER, bstrUrlContext AS STRING, bstrUrl AS STRING) AS VOID;
    HELPSTRING "A new, hidden, non-navigated WebBrowser window is needed."
    * add user code here
    this.logit(PROGRAM())
    ENDPROC



    ***********


    ENDDEFINE






  • Anonymous
    September 08, 2005
    That worked great Benjamin! Thank you :)
  • Anonymous
    June 14, 2006
    The EventHandler function allows you to connect some code to an object’s event interface. For example,...
  • Anonymous
    October 09, 2007
    PingBack from http://foxpro.ntsl119.com/scr/archives/522