代码之家  ›  专栏  ›  技术社区  ›  GHad

Visual Basic(Visual Studio 2005)将输入流重定向到进程

  •  2
  • GHad  · 技术社区  · 15 年前

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

    • 我的包装名为:例如someapp.exe arg1 arg2
    • 登录someapp.log:arg1 arg2
    • 调用原始可执行文件:ou someapp.exe arg1 arg2
    • 必须将任何控制台输入记录并转发到\someapp process inputstream
    • 必须记录来自someapp进程的任何输出和错误流

    好吧,我现在被困在第四点:

            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结束后,我可以读取并出错流来记录它,但我仍然需要为进程提供自己的包装器输入,我希望读取并出错流。

    感谢您提供信息/示例

    2 回复  |  直到 15 年前
        1
  •  1
  •   GHad    15 年前

    好的,这里的解决方案…

    所需变量:

    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()
    

    日志是另一个要记录到文件的子文件,execute是保存初始日志中的someapp.exe的变量。我仍然不知道输入流线程的控制台是否工作正常,因为我打包的应用程序没有看起来的输入。希望有人发现错误…

    为了我的目的,嗯,它就像我需要它一样工作

    格雷茨 加沙地带

    主管道内的代码

        2
  •  0
  •   RobS    15 年前

    写应用程序怎么样?

    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)