如何实现命令行<实时>回显?
我最近需要在 vb 程序中执行命令行程序并回县信息,而且是实时回显。在网上我找到以下实现代码,但是执行该代码后,要等命令行程序运行完成并结束后才会将信息回显出来。如运行 ping 127.0.0.1,要等几秒后 ping 完了才将信息全部显示出来。我希望能改一下代码,使得程序能实时回显,就像真的是在 cmd 里运行 ping 一样显示信息。请问该如何修改下面的代码,谢谢!' 这是另外一个采用VB管道技术的代码,可以通过它取得命令行回显。
Option Explicit
' Code written by JoshT. Use at your own risk
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&
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 ret 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 and check for success
' note that we use the default buffer size
' this could cause problems if the process tries to write more than this buffer size
ret = CreatePipe(hRead, hWrite, sa, 0&)
If ret = 0 Then
MsgBox "CreatePipe Failed"
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
ret = CreateProcess(vbNullString, CommandLine & vbNullChar, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
If ret Then
'wait until the command line finishes
' trouble if the app doesn't end, or waits for user input, etc
WaitForSingleObject pi.hProcess, INFINITE
'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&)
'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???
Erase sBuffer()
If lgSize <> 64 Then
Exit Do
End If
Loop
'close the handles of the process
CloseHandle pi.hProcess
CloseHandle pi.hThread
Else
MsgBox "CreateProcess Failed"
End If
'close pipe handles
CloseHandle hRead
CloseHandle hWrite
'return the command line output
RunCommand = Replace(strResult, vbNullChar, Empty)
End Function
Private Sub cmdExec_Click()
lblInf.Caption = RunCommand(txtCmd.Text)
End Sub
Private Sub Form_Load()
txtCmd.Text = "ping 127.0.0.1"
End Sub