[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Capturing the output of a DOS program that is launched?

Is there a way that I can start a DOS program and capture it's output so I can process it when it completes?

I am using VB6.0 SP3 and running on Windows 95/98 systems (may be on NT as well later). I need to use ShellExecute, Shell or something to execute the DOS program (got this part working) and wait for it to finish (got this working too) then read all the output it created and process it.

I know that the output can be redirected when the program is started from the command line (ie. C:\Something.exe > capture.txt) but is there a way to get this to happen when the program is started using say the SHELL command?
0
schworak
Asked:
schworak
1 Solution
 
vbyuvalCommented:
Which DOS commands do you want to execute ?
0
 
amebaCommented:
option Explicit

private Sub Form_Click()
    Dim sCommand as string, i as Integer
    ' execute command
    sCommand = "Ping "
    Shell "command.com /c " & sCommand & " > c:\info.txt"
    ' wait
    for i = 1 to 10000
        DoEvents
    next
    ' show file
    Shell "notepad c:\info.txt", vbNormalFocus
End Sub
0
 
schworakAuthor Commented:
Can you tell me why that works but just doing

Shell "Ping > myfile.txt"

Doesn't work?



I faked the same type of result by creating a dummy batch file on the fly that runs the program I am after. But it seemed so bulky.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
schworakAuthor Commented:
Oh, I am going to leave this question open just a bit longer in case someone has another answer. If not you will get the points AMEBA.
0
 
amebaCommented:
>why that works
You are not starting normal Windows program and you need 'command.com' or other interpreter - Environ("COMSPEC")

You can find out what is 'command.com':
   sCommand = "Ping "
replace with:
   sCommand = "command /?"
0
 
georgemanCommented:
Hi schworak
Try this code.
One important thing - change property
of dummy "mybat.bat" batch file on "Exit" when finish (for Win95/98 only).
Cheers
**************************************

Option Explicit
'--------------
'Wait Program Termination DLL Declarations
 Declare Function OpenProcess Lib "kernel32" _
                 (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                  ByVal dwProcessId As Long) As Long
'
 Declare Function WaitForSingleObject Lib "kernel32" _
                  (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
'
 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'--------------------

Public Command1_Click
'
 Dim sSendCommand As String
 Dim sBatFilePath As String
'
sSendCommand = "xcopy test1.txt test.txt > logfile.txt"
'
 sBatFilePath = App.Path & "\mybat.bat"
'
 Open sBatFilePath For Output As #1
     Print #1, sSendCommand
 Close #1
'
 Call fShellTerminated(sBatFilePath)
'
End Sub

Public Sub fShellTerminated(ByVal sCommand As String)
'
'Start Foreign App and Wait Termination
'
 Dim hprog, hProc, RetVal As Long
 Const SYNCHRONIZE = &H100000
 Const INFINITE = &HFFFF
'
 On Error GoTo Err_fShellTerminated
'

'Start foreign program
 hprog = Shell(sCommand, vbHide) 'returns taskID

'Get process handle
 hProc = OpenProcess(SYNCHRONIZE, False, hprog)

'Wait until the process terminates
 If hProc <> 0 Then
   RetVal = WaitForSingleObject(hProc, INFINITE)
   CloseHandle hProc
 End If

'Normal Exit
 Exit Sub
 
'Error Handler
Err_fShellTerminated:
'--------------------
'
End Sub

0
 
schworakAuthor Commented:
I already had the code to make me wait. I am using the Command.com /c method. Thanks though
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now