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