1 #Region "Microsoft.VisualBasic::2c82045a56ccf9a6611a6f19d9df9155, Microsoft.VisualBasic.Core\ApplicationServices\Terminal\PipelineCLI.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module PipelineCLI
35     
36     '         Sub: Start
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.IO
44 Imports System.Runtime.CompilerServices
45
46 Namespace Terminal
47
48     ''' <summary>
49     ''' a | b - 管道命令在读写方面更加适合于文本数据,由于省去了IO的时间,故而效率较高
50     ''' </summary>
51     Public Module PipelineCLI
52
53         ''' <summary>
54         ''' 使用管道的方法启动下游的应用程序
55         ''' </summary>
56         ''' <param name="app"></param>
57         ''' <param name="args"></param>
58         ''' <remarks>
59         ''' http://stackoverflow.com/questions/30546522/how-to-use-a-pipe-between-two-processes-in-process-start
60         ''' 
61         ''' let the OS do it. ``StartInfo.FileName = "cmd"`` then prepend ``executablepath`` to params so it looks 
62         ''' the way you would enter it in a command window; 
63         ''' ``StartInfo.Arguments = params`` then start the process 
64         ''' 
65         ''' – Plutonix May 30 '15 at 15:13
66         ''' </remarks>
67         ''' 
68         <Extension>
69         Public Sub Start(buf As Stream, app As StringOptional args As String = "")
70             Dim Proc As New Process
71
72             Proc.StartInfo.CreateNoWindow = True
73             Proc.StartInfo.UseShellExecute = False
74             Proc.StartInfo.FileName = app
75             Proc.StartInfo.RedirectStandardOutput = True
76             Proc.StartInfo.RedirectStandardError = True
77             Proc.StartInfo.RedirectStandardInput = True
78
79             Proc.Start()
80
81             Using writer As New BinaryWriter(Proc.StandardInput.BaseStream)
82                 Dim read As New BinaryReader(buf)
83
84                 Do While True
85                     Dim bufs As Byte() = read.ReadBytes(1024)
86                     Call writer.Write(bufs, Scan0, bufs.Length)
87                 Loop
88
89                 Call writer.Flush()
90                 Call writer.Close()
91             End Using
92
93             Call Proc.WaitForExit()
94         End Sub
95     End Module
96 End Namespace