我正在寻找一种从通过运行创建的对象中检索进程 ID 的方法:
Set ie = WScript.CreateObject("InternetExplorer.Application", "ie_")
我的问题是,有时我会看到 iexplorer
进程保持打开状态,并且在运行后并未关闭:
ie.stop
ie.Quit
我找到了一些解决方法,例如查找最新的 iexplorer
进程或查看进程名称,但这对我来说并不好,因为我有多个由不同进程并行打开的 Internet Explorer 实例而且可能是在同一时间。
这不好:
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where " _
& "Name = '"& sProcessName & "'")
我看到了this solution这可能可行,但我不知道如何在 Internet Explorer 上实现它。
最佳答案
在不调用 powershell 命令或使用 com 包装器的情况下,您可能希望通过其命令行来过滤进程。从 dcom 启动创建的 iexplorer.exe 进程具有类似 -Embedding
的命令行。关于我的意思的示例查询。
Select * from Win32_Process Where Name = 'iexplore.exe' And CommandLine Like '%-Embedding'
是的,我听到了,这将返回所有嵌入实例,因此如果有多个实例,它可能没有用。
IE 对象实例有一个返回 mainwindowhandle 的属性:HWND .
如何使用 HWND 更可靠地终止:
从 Powershell 运行命令:
Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "powershell -Command ""Get-Process | Where-Object {$_.MainWindowHandle -eq "& IE.Hwnd &"} | kill"""
WshShell.Run strCommand, vbHide, True
使用/编写一个包装 Windows API 的组件(仅限 32 位):http://www.codeproject.com/Articles/16558/DestroyWindow-in-VBScript
Set obj = CreateObject("APIWrapperCOM.APIWrapper")
obj.KillWindow IE.Hwnd
希望有帮助。
关于internet-explorer - 关闭由 WScript.CreateObject 创建的 IE 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10202512/