问题描述
我正在寻找一种方法来检索从已通过运行创建对象的进程ID:
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_")
我的问题是,有时我看到 IEXPLORER
进程是开放并运行后未关闭:
My problem is that sometimes I see that the iexplorer
process remains open and isn't closed after running:
ie.stop
ie.Quit
我发现了一些解决方法,比如寻找最新的 IEXPLORER
过程或寻找在进程名,但是这是不好的我,因为我有几个Internet Explorer的情况下,通过不同的过程并行打开和它可能是在相同的时间
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.
这是不好的:
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where " _
& "Name = '"& sProcessName & "'")
我看到可能的工作,但我不知道如何实现它的Internet Explorer。
I saw this solution that might work, but I don't know how to implement it for Internet Explorer.
推荐答案
没有调用PowerShell命令或使用COM包装,你可能希望通过它的命令行过滤器的进程。从DCOM启动创建的过程IEXPLORER.EXE像有 -Embedding
A命令行。关于我的意思示例查询。
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其中名称='IEXPLORE.EXE还有的CommandLine LIKE'%-Embedding
是的,我听说过你,这将返回所有的嵌入式实例,因此,如果有多个实例,它可能不会是有用的。
Yes I heard you, this will returns all embedded instances so it may not be useful if there are multiple instances.
这是IE浏览器对象实例有一个返回mainwindowhandle属性:的。
An IE object instance has a property that returns mainwindowhandle : HWND.
什么可以做,终止使用HWND更可靠的:
What can be done to terminate more reliable using HWND :
从Powershell的运行命令:
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
使用/写一个包装的Windows API类似的东西(仅限32位)的组成部分:的
Set obj = CreateObject("APIWrapperCOM.APIWrapper")
obj.KillWindow IE.Hwnd
希望它帮助。
这篇关于闭幕通过WScript.CreateObject创建一个IE实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!