Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to kill DOS command called from VB6

Status
Not open for further replies.

Tomeczek

Programmer
Oct 25, 2012
40
US
In my VB6 program, I’m running certain DOS command using .Exec call. I also read the command’s standard output – StdOut. In the first stage of testing, I read the entire output into some string variable (using .ReadAll). Then I realized that the information I’m looking for can be found in first five to seven lines (where the whole output is hundreds lines long). In my program I can proceed after finding the information I need, but the DOS window remains opened – I have to read the whole output to finish the command, then DOS window closes.
Is there a way to programmatically “kill” the command (like you using <ctrl>C) before command reaches its end?

Code:
Set oWShell = CreateObject("WScript.Shell")
sCmd = " ... "
Set oExe = oWShell.Exec(sCmd)
Do
    sTmp = oExe.StdOut.ReadLine()
    While Not sTmp = “”
	...
        sTmp = oExe.StdOut.ReadLine()
    Wend

[COLOR=#CC0000]At this point I’d like to kill the executed command[/color]

    ...
    ...
Loop
 
After days of testing and trying, I found the solution.
Here is the code:

Code:
Set oWShell = CreateObject("WScript.Shell")
sCmd = " ... "
Set oExe = oWShell.Exec(sCmd)
Do
    sTmp = oExe.StdOut.ReadLine()
    While Not sTmp = “”
	...
        sTmp = oExe.StdOut.ReadLine()
    Wend

    [COLOR=#CC0000][b]oExec.Terminate[/b][/color]

    ...
    ...
Loop
 
>After days of testing and trying, I found the solution.

If only you'd posted here days ago you'd have had the much, much answer faster. But glad you got it sorted yourself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top