Closing an IE instance created by WScript.CreateObject

11,440

Without calling a powershell command or using com wrapper you may want filter processes by its command line. An iexplorer.exe process which created from dcom launch has a commandline like -Embedding. A sample query about what I meant.

Select * from Win32_Process Where Name = 'iexplore.exe' And CommandLine Like '%-Embedding'

Yes I heard you, this will returns all embedded instances so it may not be useful if there are multiple instances.

An IE object instance has a property that returns mainwindowhandle : HWND.

What can be done to terminate more reliable using HWND :

Running a command from Powershell:

Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "powershell -Command ""Get-Process | Where-Object {$_.MainWindowHandle -eq "& IE.Hwnd &"} | kill"""
WshShell.Run strCommand, vbHide, True

Using / writing a component that wraps Windows APIs something like that (32 bit only): http://www.codeproject.com/Articles/16558/DestroyWindow-in-VBScript

Set obj = CreateObject("APIWrapperCOM.APIWrapper")
    obj.KillWindow IE.Hwnd

Hope it helps.

Share:
11,440
fredy
Author by

fredy

Updated on June 04, 2022

Comments

  • fredy
    fredy almost 2 years

    I'm looking for a way to retrieve the process ID from an object that was created by running:

    Set ie = WScript.CreateObject("InternetExplorer.Application", "ie_")
    

    My problem is that sometimes I see that the iexplorer process remains open and isn't closed after running:

    ie.stop
    ie.Quit
    

    I found some workarounds, like looking for the newest iexplorer process or looking at the process name, but this isn't good for me since I have several Internet Explorer instances opened in parallel by different processes and it might be on the same time.

    This is not good:

    Set colProcessList = objWMIService.ExecQuery _ 
    ("Select * from Win32_Process Where " _ 
    & "Name = '"& sProcessName & "'") 
    

    I saw this solution that might work, but I don't know how to implement it for Internet Explorer.