有没有办法在Windows中使用WSH或WMI或类似方法访问窗口句柄?

时间:2009-11-24 05:32:31

标签: windows wsh windows-scripting

有没有办法在Windows中使用WSH,WMI或类似方法访问窗口句柄?我只想标记一个窗口,总是在顶部。理想情况下,我会使用Windows脚本主机。

请注意,我不想在相关系统上安装PowerShell。我们对任何其他软件感到紧张,并且已经涉及一些VBS文件。

此致

2 个答案:

答案 0 :(得分:0)

Private Const Firefox  = "C:\App32\Mozilla Firefox\firefox.exe"
Private Const IE8      = "C:\App32\Internet Explorer\iexplore.exe"
Private Const Opera    = "C:\App32\Opera\opera.exe"
Private Const Chrome   = "C:\App32\Google\Chrome\Application\new_chrome.exe"
Private Const Safari   = "C:\App32\Safari\Safari.exe"

Private Const ShowTime  =20000
Private Const TechWait  =200
Private Const CloseWait =1000

If WScript.Arguments.Count <1 then 
           Wscript.Echo "URL not found!"         & vbCrLf &_ 
                                                   vbCrLf &_
           "ex 1: "                              & vbCrLf &_ 
       Wscript.ScriptName & " ""<URI>"       & vbCrLf &_ 
       "ex 2: "                              & vbCrLf &_ 
       Wscript.ScriptName & " ""http://www.google.com"

       Wscript.Quit(10)
End If

Set oShell=CreateObject("Wscript.Shell")

Set oCommand=oShell.exec(Firefox   & " " & WScript.Arguments(0))
FirefoxPid=oCommand.ProcessID

Set oCommand=oShell.exec(IE8       & " " & WScript.Arguments(0))
IE8Pid=oCommand.ProcessID

Set oCommand=oShell.exec(Opera     & " " & WScript.Arguments(0))
OperaPid=oCommand.ProcessID

Set oCommand=oShell.exec(Chrome    & " " & WScript.Arguments(0))
ChromePid=oCommand.ProcessID

Set oCommand=oShell.exec(Safari    & " " & WScript.Arguments(0))
SafariPid=oCommand.ProcessID


WScript.Sleep ShowTime

oShell.AppActivate(FirefoxPid)
WScript.Sleep TechWait
oShell.SendKeys "%{F4}"

WScript.Sleep CloseWait

oShell.AppActivate(IE8Pid)
WScript.Sleep TechWait
oShell.SendKeys "%{F4}"

WScript.Sleep CloseWait

oShell.AppActivate(OperaPid)
WScript.Sleep TechWait
oShell.SendKeys "%{F4}"

WScript.Sleep CloseWait

oShell.AppActivate(ChromePid)
WScript.Sleep TechWait
oShell.SendKeys "%{F4}"

WScript.Sleep CloseWait

oShell.AppActivate(SafariPid)
WScript.Sleep TechWait
oShell.SendKeys "%{F4}"

Set oCommand = Nothing
Set oShell   = Nothing

答案 1 :(得分:0)

Private Const wbemFlagReturnImmediately = 16
Private Const wbemFlagForwardOnly       = 32

Dim strComputer, objWMIService, strUserName, strPassword

UserName = ""
Password = ""
strComputer   = "."

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUserName,strPassword)

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem in colItems
    WScript.Echo objItem.Caption, objItem.ProcessId, objItem.Commandline
Next

看... wmic CLASS win32_process&gt; C:\ 4.html

Wmic进程,其中(Name ='eclipse.exe')获取CommandLine,ProcessId

如果需要新的wmi-winApi函数,可以创建mof文件并编译...

http://msdn.microsoft.com/en-us/library/windows/desktop/aa393907(v=vs.85).aspx