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

发布于 2024-08-08 12:31:05 字数 859 浏览 2 评论 0原文

我已经在网上搜索了大约三个小时,现在还没有进展。我不太了解 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 结束后我能够read out 和 err 流来记录它,但我仍然需要向进程提供我自己的包装器输入,并且我想在发生时读取和 err 流。

感谢您提供信息/示例

I've been searching around the net for roughly three hours now w/o getting forward. I don't know VB very well, but I need to create a wrapper program for any executable that logs the arguments, all input, output and err information:

  • My wrapper is called: e.g. someApp.exe arg1 arg2
  • Logs to someApp.log: arg1 arg2
  • Calls original executable: _someApp.exe arg1 arg2
  • Must log and forward any console input to _someApp process inputstream
  • Must log any output and error stream from _someApp process

Okay, I'm stuck at point 4 now:

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

After _someApp ends I am able to read out and err stream to log it, but I still need to provide my own wrappers input to the process and I want to read out and err stream as it happens.

Thanks for info/examples

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一城柳絮吹成雪 2024-08-15 12:31:05

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

所需的变量:

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

在 main 中:

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

就我的目的而言,嗯,它的工作原理就像我需要它一样

Greetz,
GHad代码

main 内的

Okay here the solution...

Variables needed:

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

Subs needed:

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

Inside main:

        ' 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 is another sub to log to file, execute is the variable holding the _someApp.exe frommy initial post. I still don't know if the console to inputstream thread dows it right, because my wrapped app had no input as it seems. May someone spotts an error...

For my purposes, hmm it works like I need it

Greetz,
GHad

Code inside main

久伴你 2024-08-15 12:31:05

写入应用程序怎么样:

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)

How about for writing to the app:

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

and for reading from the standard output:

Dim sr As IO.StreamReader = process.StandardOutput
Do
  aString = sr.ReadLine()
Loop Until (sr.EndOfStream)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文