2

我已经在网上搜索了大约三个小时,没有前进。我不太了解 VB,但我需要为任何记录参数、所有输入、输出和错误信息的可执行文件创建一个包装程序:

  • 我的包装器被称为:例如 someApp.exe arg1 arg2
  • 记录到 someApp.log:arg1 arg2
  • 调用原始可执行文件:_someApp.exe arg1 arg2
  • 必须记录任何控制台输入并将其转发到 _someApp 进程输入流
  • 必须记录来自 _someApp 进程的任何输出和错误流

好的,我现在停留在第 4 点:

        Dim p As New ProcessStartInfo
        p.FileName = execute
        p.Arguments = Command()
        p.UseShellExecute = False
        p.CreateNoWindow = True
        p.RedirectStandardInput = True
        p.RedirectStandardError = True
        p.RedirectStandardOutput = True

        Dim process As System.Diagnostics.Process
        process = Diagnostics.Process.Start(p)
        process.WaitForExit()

在 _someApp 结束后,我可以读取和错误流以记录它,但我仍然需要向进程提供我自己的包装器输入,并且我想在发生错误时读取和错误流。

感谢您提供信息/示例

4

2 回答 2

1

好的,这里的解决方案...

需要的变量:

Private process As System.Diagnostics.Process

Private threadOut As Thread

Private streamOut As System.IO.StreamReader

Private threadErr As Thread

Private streamErr As System.IO.StreamReader

Private threadIn As Thread

Private streamIn As System.IO.StreamWriter

需要的潜艇:

Private Sub ThreadTaskOut()
    Dim line
    While Not process.HasExited
        line = streamOut.ReadToEnd
        If line <> Nothing And line <> "" Then
            log("Out: " & line)
            Console.Out.Write(line)
        End If
    End While
End Sub

Private Sub ThreadTaskErr()
    Dim line
    While Not process.HasExited
        line = streamErr.ReadToEnd
        If line <> Nothing And line <> "" Then
            log("Err: " & line)
            Console.Error.Write(line)
        End If
    End While
End Sub

Private Sub ThreadTaskIn()
    Dim line
    While Not process.HasExited
        line = Console.In.ReadLine
        If line <> Nothing And line <> "" Then
            log("In: " & line)
            streamIn.WriteLine(line)
        End If
    End While
End Sub

内部主要:

        ' create process information
        Dim p As New ProcessStartInfo
        p.FileName = execute
        p.Arguments = Command()
        p.UseShellExecute = False
        p.CreateNoWindow = True
        p.RedirectStandardInput = True
        p.RedirectStandardError = True
        p.RedirectStandardOutput = True

        ' log process start
        log("Execute: " & execute & " " & Command())

        ' start process
        process = Diagnostics.Process.Start(p)

        ' start thread for output stream
        streamOut = process.StandardOutput
        threadOut = New Thread(AddressOf ThreadTaskOut)
        threadOut.IsBackground = True
        threadOut.Start()

        ' start thread for error stream
        streamErr = process.StandardError
        threadErr = New Thread(AddressOf ThreadTaskErr)
        threadErr.IsBackground = True
        threadErr.Start()

        ' start thread for input stream
        streamIn = process.StandardInput
        threadIn = New Thread(AddressOf ThreadTaskIn)
        threadIn.IsBackground = True
        threadIn.Start()

        ' wait for the process to finish
        process.WaitForExit()

log 是另一个记录到文件的 sub,execute 是保存我最初帖子中的 _someApp.exe 的变量。我仍然不知道输入流线程的控制台是否正确,因为我包装的应用程序看起来没有输入。可能有人发现错误...

出于我的目的,嗯,它就像我需要它一样工作

格瑞兹,GHad

main里面的代码

于 2009-10-22T15:30:08.477 回答
0

如何写入应用程序:

Dim sw as IO.StreamWriter = process.StandardInput
sw.WriteLine("Boo")

并从标准输出中读取:

Dim sr As IO.StreamReader = process.StandardOutput
Do
  aString = sr.ReadLine()
Loop Until (sr.EndOfStream)
于 2009-10-22T13:40:19.157 回答