Link to home
Start Free TrialLog in
Avatar of schworak
schworak

asked on

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?
Avatar of vbyuval
vbyuval

Which DOS commands do you want to execute ?
ASKER CERTIFIED SOLUTION
Avatar of ameba
ameba
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of schworak

ASKER

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.
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.
>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 /?"
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

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