How to auto close VBScript thousands of MsgBox's
I have a rogue vbscript that went a little cr开发者_如何学Pythonazy tracing output and now I have thousands of message boxes to close. I can hold down the Enter key and close lots of them but that still takes several minutes. I could reboot but then I have to open all my apps again. Is there a quick way to auto close all the message boxes. I tried looking in task manager but it appears that the process that spawned the boxes has long sinced finished. Any ideas?
Not sure how you can have orphaned msgbox windows, you should still have cscript.exe or wscript.exe in your running processes list. The following should terminate the underlying process and close your msgboxes:
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
Obviously, change wscript.exe. to cscript.exe if that's what you're using.
Always start your vbscript with cscript.exe
instead of wscript.exe
. cscript outputs to the console, not the GUI. Alternatively, you could use an application such as Push The Freakin' Button to automate the button clicks.
If you're using explicit MsgBox
calls, then using cscript won't help you. To use cscript as a solution, you would need to change MsgBox
to Wscript.Echo
calls.
This wont help your immediate problem, but you may want to change your default script host to Cscript, which will prevent this problem in the future. See: this technet article.
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
I found this code here
To close all the windows and stop all processes in one go, why not just open a command prompt window and type:
TASKKILL /F /IM cmd.exe /T
or
TASKKILL /F /IM wscript.exe /T
This will immediately terminate all cmd.exe or wscript.exe processes... If it has to be within a script, you can call it like WshShell.Run "TASKKILL /F /IM cmd.exe /T"
This is much more simpler and efficient...
精彩评论