Link to home
Start Free TrialLog in
Avatar of sakuya_su
sakuya_su

asked on

Opinions needed on best approach, console application text

Hi,

I would like suggestions on the best aproach on the following problem:

  My program calls an console application, the console application then outputs onto the screen its current status, there is alot of text in it, it prints one line every 1/3 sec or so, now I would like to be able to report the status of the program in my own VB application(the console is hidden).

 - I dont think piping to a file then reading it is a good idea, i tried that and through the process, it generates a file thats 5 mb in size..

The solution that is adopted will get full points

Thanks in advanced

Avatar of Nightman
Nightman
Flag of Australia image

Do you need all the data, or simply the last status?
If just the last, consider simply maintaining a one-line status file, which the VB application can read.
You could also write the last status to the registry, and have the VB application read from there.
Avatar of sakuya_su
sakuya_su

ASKER

I have no control over the console application

yes I only need the last line status, can you please show me how to pipe only the latest line into the file?

thanks
Thought the console app was yours. The output is simply a buffer, so theoretically you can simply loop reading the buffer.

Have a look at this sample:
http://www.vbforums.com/showthread.php?t=152175
Theoretically you can simply loop and read more from the output.
that old question was also asked by me, I cannot use that method now because the file generated in the end is too large
lol. Didn't even notice. Ok - I will look deeper.
OK, I have taken the sample from that first link and modified it o run in an infinite loop - this will always simply print the last 64 bytes from the output buffer. You can test it by creating a file C:\test.bat
in the bat file simply enter this:
dir *.* /s

You can take all of this code and simply paste it into a new standard.exe to test

Option Explicit
Option Base 0

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
   (ByVal lpApplicationName As String, _
    ByVal lpCommandLine As String, _
    lpProcessAttributes As SECURITY_ATTRIBUTES, _
    lpThreadAttributes As SECURITY_ATTRIBUTES, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    lpEnvironment As Any, _
    ByVal lpCurrentDirectory As String, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long

Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, _
    lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, _
    lpOverlapped As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, _
    phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, _
    ByVal nSize As Long) As Long

Private Type STARTUPINFO
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
End Type

Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type

Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
End Type

Private Const NORMAL_PRIORITY_CLASS As Long = &H20&

Private Const STARTF_USESTDHANDLES As Long = &H100&
Private Const STARTF_USESHOWWINDOW As Long = &H1&
Private Const SW_HIDE As Long = 0&

Private Const INFINITE As Long = &HFFFF&


Private Sub Form_Load()
  RunCommand "C:\test.bat"
End Sub

Public Function RunCommand(CommandLine As String) As String
    Dim si As STARTUPINFO 'used to send info the CreateProcess
    Dim pi As PROCESS_INFORMATION 'used to receive info about the created process
    Dim retval As Long 'return value
    Dim hRead As Long 'the handle to the read end of the pipe
    Dim hWrite As Long 'the handle to the write end of the pipe
    Dim sBuffer(0 To 63) As Byte 'the buffer to store data as we read it from the pipe
    Dim lgSize As Long 'returned number of bytes read by readfile
    Dim sa As SECURITY_ATTRIBUTES
    Dim strResult As String 'returned results of the command line
   
    'set up security attributes structure
    With sa
        .nLength = Len(sa)
        .bInheritHandle = 1& 'inherit, needed for this to work
        .lpSecurityDescriptor = 0&
    End With
   
    'create our anonymous pipe an check for success
    '   note we use the default buffer size
    '   this could cause problems if the process tries to write more than this buffer size
    retval = CreatePipe(hRead, hWrite, sa, 0&)
    If retval = 0 Then
        Debug.Print "CreatePipe Failed"
        RunCommand = ""
        Exit Function
    End If
   
    'set up startup info
    With si
        .cb = Len(si)
        .dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
        .wShowWindow = SW_HIDE
'        .hStdInput = GetStdHandle(STD_INPUT_HANDLE)
        .hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
'        .hStdError = GetStdHandle(STD_ERROR_HANDLE)
    End With
   
    'run the command line and check for success
    retval = CreateProcess(vbNullString, _
                            CommandLine & vbNullChar, _
                            sa, _
                            sa, _
                            1&, _
                            NORMAL_PRIORITY_CLASS, _
                            ByVal 0&, _
                            vbNullString, _
                            si, _
                            pi)
    If retval Then
        'wait until the command line finishes
        '   trouble if the app doesn't end, or waits for user input, etc
        'WaitForSingleObject pi.hProcess, INFINITE
       
        Do While 1 = 1
        'read from the pipe until there's no more (bytes actually read is less than what we told it to)
        'Do While ReadFile(hRead, sBuffer(0), 64, lgSize, ByVal 0&)
          ReadFile hRead, sBuffer(0), 64, lgSize, ByVal 0&
            'convert byte array to string and append to our result
            strResult = strResult & StrConv(sBuffer(), vbUnicode)
            'TODO = what's in the tail end of the byte array when lgSize is less than 64???
            Debug.Print strResult
           
            Erase sBuffer()
            If lgSize <> 64 Then
              Exit Do
            End If
            Sleep 100
        Loop
       
        'close the handles of the process
        CloseHandle pi.hProcess
        CloseHandle pi.hThread
    Else
        Debug.Print "CreateProcess Failed" & vbCrLf
    End If
   
    'close pipe handles
    CloseHandle hRead
    CloseHandle hWrite
   
    'return the command line output
    RunCommand = Replace(strResult, vbNullChar, "")
End Function

Hi,

Sorry for taking soo long to get back.

there are a few problems with the above code:
The program shells is very unstable, they tend to just stop after a few seconds (I'm shelling MEncoder, which constantly spits out messages for about 20 minutes)

the coding is too slow, I'm getting about 10% of the usual speed of the original program, I removed sleep 100 however the shell became even more unstable?
ASKER CERTIFIED SOLUTION
Avatar of Nightman
Nightman
Flag of Australia 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