bartteems
asked on
How to run a Command line Program without taking focus from current user window?
I have a vbscript that repeatedly (every 30 seconds) needs to run a commandline program. The script is running in the background without any presence on the monitor. The program takes less than a second to run and my script captures the return code. My problem is every time I run it, it creates a DOS box window (which is ugly, but Ok) and it steals the focus. So if the user is running word and they are typing along, all of a sudden their keystrokes are lost. Does anyone know a way to initiate this process without this occurring? Thanks
Here is my code that I use
Function RunSafeBootSTATE
On Error Resume Next
Dim WshShell, oExec, StartTime
Set WshShell = CreateObject("WScript.Shel l")
WScript.Sleep 10000 ' allow time for installer to wrap up previous
Dim CmdLine
CmdLine = "C:\Program Files\XXBoot\hjklmcl.exe -command:getcryptstate"
Set oExec = WshShell.Exec(CmdLine)
If Err.Number<> 0 Then
LogMessage "Error Starting Command: " & Err.Description & " (" & Err.Number & ")"
RunSafeBootSTATE = -1
Exit Function
End If
StartTime = Now
Do While oExec.Status = 0
WScript.Sleep 1000
If DateDiff("m",StartTime,Now ) > 10 Then
' error
LogMessage "Failed to complete in 10 minutes"
RunSafeBootSTATE = -1
Exit Function
End If
Loop
' We Completed Command
If oExec.ExitCode = 0 Then
' We are Done, let's check results
Else
' We Failed
LogMessage "Returned Error Code: " & oExec.ExitCode
RunSafeBootSTATE = -1
Exit Function
End If
Dim CmdResult
CmdResult = oExec.StdOut.ReadAll()
'continues to process CmdResult
End Function
Here is my code that I use
Function RunSafeBootSTATE
On Error Resume Next
Dim WshShell, oExec, StartTime
Set WshShell = CreateObject("WScript.Shel
WScript.Sleep 10000 ' allow time for installer to wrap up previous
Dim CmdLine
CmdLine = "C:\Program Files\XXBoot\hjklmcl.exe -command:getcryptstate"
Set oExec = WshShell.Exec(CmdLine)
If Err.Number<> 0 Then
LogMessage "Error Starting Command: " & Err.Description & " (" & Err.Number & ")"
RunSafeBootSTATE = -1
Exit Function
End If
StartTime = Now
Do While oExec.Status = 0
WScript.Sleep 1000
If DateDiff("m",StartTime,Now
' error
LogMessage "Failed to complete in 10 minutes"
RunSafeBootSTATE = -1
Exit Function
End If
Loop
' We Completed Command
If oExec.ExitCode = 0 Then
' We are Done, let's check results
Else
' We Failed
LogMessage "Returned Error Code: " & oExec.ExitCode
RunSafeBootSTATE = -1
Exit Function
End If
Dim CmdResult
CmdResult = oExec.StdOut.ReadAll()
'continues to process CmdResult
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all very much for the comments. I tried the start method without success, so I am going to use the run method. I used .exec for two reason first to monitor (which I know how todo via the WMI process). THe second reason is my use of the STDin data, but I am going to try and pipe it or redirect it to a file, just more of a pain and error prone to get the disk file data working reliably. Thanks again
Bart
Bart
No problem. Good luck with it.
Rob.
Rob.
Similar problems pertain to using the .Run method with the intWindowStyle argument instead of .Exec.
http://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
You might have to see if the program can write output to a temporary file, then read the result from there.
Open in new window