Solved

How to run a Command line Program without taking focus from current user window?

Posted on 2008-06-19
5
498 Views
Last Modified: 2008-06-19
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.Shell")
   
    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
0
Comment
Question by:bartteems
5 Comments
 
LVL 17

Accepted Solution

by:
Jared Luker earned 250 total points
Comment Utility
If you change your code to use the .run method, then you have several options with what you do with the window:

http://www.devguru.com/technologies/wsh/quickref/wshshell_Run.html
0
 
LVL 38

Expert Comment

by:Shift-3
Comment Utility
You could try running the command with START /MIN, though this might mess up the return code.  It also depends on the program supporting it.

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.


CmdLine = "%comspec% /c start /min C:\Program Files\XXBoot\hjklmcl.exe -command:getcryptstate"

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 250 total points
Comment Utility
Hi guys, just throwing in a quick idea here...

The Run method would be better suited to hiding the command, but it looks like bartteems is using the Exec so they can monitor the process for 10 minutes, which the Run command won't natively allow.

You could, however, add a bit after
WshShell.Run CmdLine, 7, False

that queries the Win32_Process class for the hjklmcl.exe process, to see if it is still running 10 minutes late......what are your thoughts?

Regards,

Rob
0
 

Author Comment

by:bartteems
Comment Utility
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
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
No problem. Good luck with it.

Rob.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now