views:

246

answers:

2

Hi community.

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 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 
+2  A: 

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.)

Steven Sudit
can you provide code to focus the window?
Luiscencio
You don't want to focus the window. You want to find its HWND based on its title and then send a message. See http://stackoverflow.com/questions/1391661/using-wm-close-in-c
Steven Sudit
I think what Alt-F4 does is to send a `WM_QUIT`. No need to emulate keystrokes.
ssg
@ssg: I believe that WM_CLOSE is correct, not WM_QUIT.
Steven Sudit
Perhaps, but emulating Alt-F4 is definitely wrong :)
ssg
@ssg: No argument there.
Steven Sudit
I would appreciate it if the downvoter would explain their reasoning.
Steven Sudit
+2  A: 

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 
Joe
thanks just had to tweak it a little....
Luiscencio