Closing an IE instance created by WScript.CreateObject

夙愿已清 提交于 2019-12-24 02:33:19

问题


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.


回答1:


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.




回答2:


I think that you're looking for this one I've tested it in a Powershell Object so It should be the same thing.

$ie.Quit()


来源:https://stackoverflow.com/questions/10202512/closing-an-ie-instance-created-by-wscript-createobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!