【问题标题】:How to auto close VBScript thousands of MsgBox's如何自动关闭 VBScript 数千个 MsgBox
【发布时间】:2011-09-09 18:52:03
【问题描述】:

我有一个流氓 vbscript,它的跟踪输出有点疯狂,现在我有数千个消息框要关闭。我可以按住 Enter 键并关闭其中很多,但这仍然需要几分钟。我可以重新启动,但我必须再次打开我的所有应用程序。有没有一种快速的方法来自动关闭所有的消息框。我尝试在任务管理器中查看,但似乎生成这些框的过程早已完成。有什么想法吗?

【问题讨论】:

  • 请注意,我确实使用 cscript 来运行 vbs,但 MsgBox cmds 是硬编码的,直到完成后我才意识到这一点。

标签: windows vbscript msgbox


【解决方案1】:

不确定如何拥有孤立的 msgbox 窗口,您的运行进程列表中应该仍然有 cscript.exe 或 wscript.exe。以下应该终止底层进程并关闭您的 msgbox:

strComputer = "."
strProcessToKill = "wscript.exe" 

SET objWMIService = GETOBJECT("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\cimv2") 

SET colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")

FOR EACH objProcess in colProcess
    objProcess.Terminate()
NEXT 

显然,更改 wscript.exe。到 cscript.exe 如果这是你正在使用的。

【讨论】:

    【解决方案2】:

    始终以cscript.exe 而不是wscript.exe 开始您的vbscript。 cscript 输出到控制台,而不是 GUI。或者,您可以使用诸如Push The Freakin' Button 之类的应用程序来自动单击按钮。

    如果您使用显式的 MsgBox 调用,那么使用 cscript 将无济于事。要使用 cscript 作为解决方案,您需要将 MsgBox 更改为 Wscript.Echo 调用。

    【讨论】:

      【解决方案3】:

      这不会帮助您解决当前的问题,但您可能希望将默认脚本主机更改为 Cscript,这样可以防止将来出现此问题。请参阅:this technet article

      【讨论】:

        【解决方案4】:
        Public Class Form1
        
        Private m_Title As String
        
        'Windows API
        
        Private Declare Function PostMessage Lib "user32" _
        Alias "PostMessageA" (ByVal hWnd As Int32, _
        ByVal wMsg As Int32, _
        ByVal wParam As Int32, _
        ByVal lParam As Int32) As Int32
        
        
        Declare Function SendMessage Lib "USER32" _
        Alias "SendMessageA" (ByVal hWnd As Int32, _
        ByVal Msg As Int32, _
        ByVal wParam As Int32, _
        ByVal lParam As Int32) As Int32
        
        
        Private Declare Function FindWindow Lib "user32" _
        Alias "FindWindowA" (ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Int32
        
        Private Const WM_CLOSE As Int32 = &H10
        
        
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click
        
        m_Title = "Auto Close Msg"
        
        Me.Timer1.Interval = 2000 'timer1 placed on form
        Me.Timer1.Start()
        
        
        MsgBox("Auto close in 2 seconds", MsgBoxStyle.OkOnly, m_Title)
        
        End Sub
        
        
        Private Sub CloseMSGBOX()
        'Use Windows API to find and close the message box
        '
        'http://msdn.microsoft.com/en-us/library/…
        '#32770 The class for a dialog box. 
        'http://msdn.microsoft.com/en-us/library/…
        '
        'http://msdn.microsoft.com/en-us/library/…
        '
        Dim hWnd, retval As Int32
        Dim WinTitle As String
        WinTitle = m_Title '<- Title of Window
        
        hWnd = FindWindow("#32770", WinTitle) 'Get the msgBox handle
        retval = PostMessage(hWnd, WM_CLOSE, 0, 0) ' Close the msgBox
        
        End Sub
        
        
        Private Sub Timer1_Tick(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Timer1.Tick
        
        CloseMSGBOX()
        
        End Sub
        End Class
        

        我找到了这个代码here

        【讨论】:

        • Declare 不受 VBScript 支持。
        • Dim x As {Type} 也不是。这个答案不是 VBScript!
        【解决方案5】:

        要一次性关闭所有窗口并停止所有进程,何不直接打开命令提示符窗口并输入:

        TASKKILL /F /IM cmd.exe /T
        

        TASKKILL /F /IM wscript.exe /T
        

        这将立即终止所有 cmd.exe 或 wscript.exe 进程...如果它必须在脚本中,您可以将其称为 WshShell.Run "TASKKILL /F /IM cmd.exe /T"

        这样更简单高效...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-17
          • 2010-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多