How to "End Task" not "Kill" or "Terminate"?
I have a 3G card to provide internet to a remote computer... I have to run a program(provided with the card) to establish the connection... since connections suddenly is lost I wrote a script that Kills the program and reopens it so that the connection is reestablished, there are certain versions of this program that don't kill the connection when killed/terminated, just when closed properly.
so I am looking for a script or program that "Properly Closes" a window so I can close it and reopen it in case the connection is lost.
this is the code that kills the program
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'Telcel3G.exe'"
Set 开发者_StackOverflow中文版objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer
WScript.Quit
If you have to use VBScript, one easy way would be to activate the app and then use SendKeys to restore it if it's minimized and send it "alt-f4". It's hacky, but it may be easier than finding the main window's handle, and it might work well enough for you.
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'Telcel3G.exe'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
dim WshShell
set WshShell = CreateObject("WScript.Shell")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
WshShell.AppActivate objProcess.ProcessId
WScript.Sleep 1000
WshShell.Sendkeys "% r"
WScript.Sleep 1000
WshShell.Sendkeys "%{F4}"
Next
As I understand it, the basic idea is to shut the process down cleanly, as opposed to abruptly terminating it. One way would be to post a Windows message to the main window with WM_CLOSE.
(You might be able to simulate this by sending keystrokes corresponding to Alt+F4, but it's best to just send WM_CLOSE.)
精彩评论