IE导航到本地文件时找不到基于HWND的iexplorer进程?
Posted
技术标签:
【中文标题】IE导航到本地文件时找不到基于HWND的iexplorer进程?【英文标题】:Cannot find iexplorer process based on HWND when IE is navigated to a local file? 【发布时间】:2018-10-31 15:01:57 【问题描述】:我正在尝试查找 Internet Explorer com-object 的进程,当 IE com-object 导航到类似 google.nl 的 URL,但是当它导航到本地 html 文件时,它不会。
关于函数的一些信息:
-
它创建了一个新的 ieplorer com-object
它更改了 iexporer com-object 的一些属性。
它使用来自 ieplorer com-object 的导航功能和指定的 url
它根据 HWND 返回此 com 对象的进程对象。
问题存在于第 4 步,这是一个简单的 where 逻辑:
Get-Process -Name iexplore | Where-Object $_.MainWindowHandle -eq $ie.HWND
我尝试了几件事,甚至延迟构建,但不知何故它不起作用。 如果通过直接指定 HWND 从函数外部尝试逻辑,它确实有效,例如:
PS C:\> Start-IELockedDown localhost
HWND = 659256
HWND Type = int
IE Processes:
@Name=iexplore; MainWindowHandle=919956
@Name=iexplore; MainWindowHandle=397090
@Name=iexplore; MainWindowHandle=659256 #matching Entry
@Name=iexplore; MainWindowHandle=0
@Name=iexplore; MainWindowHandle=69090
@Name=iexplore; MainWindowHandle=0
IE with correct HWND
#Nothing?
After this comes the RETURN:
#Also Nothing!
PS C:\> Get-Process -Name iexplore | Where-Object $_.MainWindowHandle -eq 659256
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
582 42 9656 30940 0,50 10260 2 iexplore
调试示例:
1 当我使用像 Start-IELockedDown google.nl
这样的外部和工作网站运行命令时,它实际上可以工作:
HWND = 921524
IE Processes:
@Name=iexplore; MainWindowHandle=921524 #Matching entry
@Name=iexplore; MainWindowHandle=724706
@Name=iexplore; MainWindowHandle=69090
@Name=iexplore; MainWindowHandle=0
IE with correct HWND
@Name=iexplore; MainWindowHandle=921524 #Logic works
After this comes the RETURN:
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
480 31 7092 26848 0,22 18912 2 iexplore
2 当我使用文件路径而不是像 Start-IELockedDown (Get-Item .\blank-page.html | Select-Object -ExpandProperty FullName)
这样的外部网站时,逻辑似乎中断了:
HWND = 3015698
IE Processes:
@Name=iexplore; MainWindowHandle=0
@Name=iexplore; MainWindowHandle=3015698 #Matching Entry
@Name=iexplore; MainWindowHandle=1248958
@Name=iexplore; MainWindowHandle=69090
@Name=iexplore; MainWindowHandle=0
IE with correct HWND
#Nothing?
After this comes the RETURN:
#Also nothing!
3 当我针对 localhost 运行它时,它没有响应,因为我没有像这样运行的本地网络服务器 Start-IELockedDown LocalHost
它给了我同样的问题:
HWND = 986890
IE Processes:
@Name=iexplore; MainWindowHandle=986890 #Matching Entry
@Name=iexplore; MainWindowHandle=1051034
@Name=iexplore; MainWindowHandle=69090
@Name=iexplore; MainWindowHandle=2361678
@Name=iexplore; MainWindowHandle=0
IE with correct HWND
#Nothing?
After this comes the RETURN:
#Also Nothing!
完整代码:
function Start-IELockedDown
<#
.SYNOPSIS
Open IE without navigation controlls.
.DESCRIPTION
Long description
.EXAMPLE
PS C:\> Invoke-IELockedDown -URL "http://localhost:8080/"
Opens a Internet Explorer browser window without navigational controlls that navigates to the specified URL.
.INPUTS
None
.OUTPUTS
InternetExplorer Process Object.
.NOTES
General notes
#>
[CmdletBinding()]
param (
[string] $URL
)
# Create IE com object
$ie = New-Object -com InternetExplorer.Application
# Turns off the unnecessary menus and tools and sets the window as resizable
$ie.AddressBar = $false
$ie.MenuBar = $false
$ie.ToolBar = $false
$ie.Resizable = $true
$ie.StatusBar = $false
# Sets the size of the window and make it visible
$ie.Top = 20
$ie.Left = 20
$ie.Width = 1280
$ie.Height = 1024
$ie.Visible = $true
## Navigate the browser to the specified URL.
$ie.Navigate($URL)
# For Debugging / the *** Guru's
Write-host "HWND = $($ie.HWND)"
Write-Host "IE Processes:"
Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Write-host
Write-Host "IE with correct HWND"
Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Where-Object $_.MainWindowHandle -eq $ie.HWND | Write-Host
Write-Host "After this comes the RETURN:"
# Return the Process Object for the IE Com Object
return Get-Process -Name iexplore | Where-Object $_.MainWindowHandle -eq $ie.HWND
【问题讨论】:
我无法使用 PowerShell 5.1 在 Win10 上复制该问题 感谢@TheMadTechnician,我正在使用 Powershell 5.1 aswel 运行 Win10,我将尝试使用不同的系统来查看它是否只是本地的。 我刚刚在一个干净的完全更新的 Windows 10 机器上用干净的 Powershell v5.1.17134.48 控制台试了一下,我遇到了同样的问题,远程 url 有效,本地文件无效。 啊,好的,我能够复制,但只能在未以管理员身份运行的 PowerShell 会话中。您是否可以为此以管理员身份运行 PowerShell? 我刚刚尝试过,我看到了同样的行为。 【参考方案1】:此问题似乎只存在于 PowerShell 的非提升会话中。如果您想解决此问题,您可以运行提升的 PowerShell 会话(即以管理员身份运行),或者您可以在导航到 URL 之前捕获 HWND,并引用它。
function Start-IELockedDown
<#
.SYNOPSIS
Open IE without navigation controlls.
.DESCRIPTION
Long description
.EXAMPLE
PS C:\> Invoke-IELockedDown -URL "http://localhost:8080/"
Opens a Internet Explorer browser window without navigational controlls that navigates to the specified URL.
.INPUTS
None
.OUTPUTS
InternetExplorer Process Object.
.NOTES
General notes
#>
[CmdletBinding()]
param (
[string] $URL
)
# Create IE com object
$ie = New-Object -com InternetExplorer.Application
$HWND = $ie.HWND
# Turns off the unnecessary menus and tools and sets the window as resizable
$ie.AddressBar = $false
$ie.MenuBar = $false
$ie.ToolBar = $false
$ie.Resizable = $true
$ie.StatusBar = $false
# Sets the size of the window and make it visible
$ie.Top = 20
$ie.Left = 20
$ie.Width = 1280
$ie.Height = 1024
$ie.Visible = $true
## Navigate the browser to the specified URL.
$ie.Navigate($URL)
# For Debugging / the *** Guru's
Write-host "HWND = $($ie.HWND)"
Write-Host "IE Processes:"
Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Write-host
Write-Host "IE with correct HWND"
Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Where-Object $_.MainWindowHandle -eq $HWND | Write-Host
Write-Host "After this comes the RETURN:"
# Return the Process Object for the IE Com Object
return Get-Process -Name iexplore | Where-Object $_.MainWindowHandle -eq $HWND
【讨论】:
感谢它现在工作的答案,在提升的会话中运行它对我来说是没有选择的,所以提供的代码更改将是完美的。以上是关于IE导航到本地文件时找不到基于HWND的iexplorer进程?的主要内容,如果未能解决你的问题,请参考以下文章
尝试使用导航器页面访问时找不到 Flutter scoped_model