In an earlier post I was trying to use dhGetObject() to acquire object handles to running instances of Internet Explorer. I could not get this to work (and would still like to) but in the meantime, Amundo has pointed me to an alternative PowerBasic solution which I have translated to PureBasic in case anyone is interested.
Before running the PureBasic code (in Debug mode), I suggest you run two instances of Internet Explorer (showing two different websites) and an instance of Windows Explorer.
Code: Select all
; IE Instances AKJ 23-May-07
; Enumerate the ShellWindows collection and
; identify running instances of Internet Explorer
; Converted from: www.forum.it-berater.org/index.php?topic=575.0
; The comments are mainly the text of the original PowerBasic code
; #COMPILE EXE
; #DIM ALL
; #INCLUDE "WIN32API.INC"
EnableExplicit
IncludePath #PB_Compiler_Home+"Examples\DispHelper_Include"
XIncludeFile "DispHelper_Include.pb"
Procedure Warn(msg$)
MessageRequester("Warning", msg$, #MB_ICONWARNING)
EndProcedure
; FUNCTION PBMAIN () AS LONG
; LOCAL oShellApp AS DISPATCH
; LOCAL oShellWindows AS DISPATCH
; LOCAL oIE AS DISPATCH
; LOCAL vRes AS VARIANT
; LOCAL nCount AS LONG
; LOCAL i AS LONG
; LOCAL vIdx AS VARIANT
Define oShellApp, oShellWindows, oIE, oDoc ; Objects
Define nCount, i, vRes, program$, error, OK.b=#True
dhInitializeImp()
dhToggleExceptions(#True)
; SET oShellApp = NEW DISPATCH IN "Shell.Application"
; IF ISFALSE ISOBJECT(oShellApp) THEN EXIT FUNCTION
oShellApp = dhCreateObject("Shell.Application")
If oShellApp=0
OK = #False
Warn("Could not create Shell.Application object")
EndIf
; OBJECT CALL oShellApp.Windows TO vRes
; SET oShellWindows = vRes
; IF ISFALSE ISOBJECT(oShellWindows) THEN EXIT FUNCTION
If OK
error = dhGetValue("%o", @oShellWindows, oShellApp, ".Windows")
If error
OK = #False
Warn("Shell.Windows method failed")
EndIf
EndIf
; OBJECT GET oShellWindows.Count TO vRes
; nCount = VARIANT#(vRes)
If OK
error = dhGetValue("%d", @nCount, oShellWindows, ".Count")
If error
OK = #False
Warn("ShellWindows.Count failed")
EndIf
EndIf
If OK
Debug "Shell windows count = "+Str(nCount)
; FOR i = 0 TO nCount - 1
For i = 0 To nCount - 1
; vIdx = i AS LONG
; OBJECT CALL oShellWindows.Item(vIdx) TO vRes
; SET oIE = vRes
error = dhGetValue("%o", @oIE, oShellWindows, ".Item(%d)", i)
If error
OK = #False
Warn("ShellWindows.Item() method failed")
EndIf
; IF ISTRUE ISOBJECT(oIE) THEN
; OBJECT GET oIE.FullName TO vRes
; MSGBOX VARIANT$(vRes)
; END IF
If OK
Debug ""
Debug "Shell object handle = $"+Hex(oIE)
error = dhGetValue("%T", @vRes, oIE, ".FullName")
If error
Warn("IE.FullName method failed")
Else
program$ = GetFilePart(PeekS(vRes))
Debug "Program = "+program$
If UCase(program$)="IEXPLORE.EXE" ; If Internet Explorer
; Get IE title text (code added by AKJ)
error = dhGetValue("%o", @oDoc, oIE, ".Document") ; Set oDoc = ieApp.Document
If error
Warn("IE.Document method failed")
Else
error = dhGetValue("%T", @vRes, oDoc, ".Title") ; title = oDoc.title
If error
Warn("IE.Document.Title method failed")
Else
Debug "IE Title = "+PeekS(vRes)
EndIf
EndIf
Else
Debug "(Not Internet Explorer)"
EndIf ; IE
EndIf
EndIf ; OK
; NEXT
Next i
EndIf ; OK
; END FUNCTION
dhFreeString(vRes)
dhReleaseObject(oIE)
dhReleaseObject(oShellWindows)
dhReleaseObject(oShellApp)
dhUninitialize()
End