想在windows 2003下把命令行程序如ping的输出进行截获,这样我就可以在一个GUI窗口里输出了
请大家指教!

解决方案 »

  1.   

    ping 192.168.0.1 >d.txt
    可以使用 > 把结果输出到 D.TXT
      

  2.   

    如果已经生成了一个d.txt,那么可以使用  >> 把新的输出加在这个文件的后面
      

  3.   

    1.重定向输出
    使用Visual Basic的Shell函数,PROCESSID=Shell ("MY_APPLICATION " & APPLICATION_PRAR  & "> TEMP_FILE", vbHide)
    其中APPLICATION_PRAR为应用程序的命令行参数。2.使用Win32API CreateProcess
    Declare Function CreateProcess Lib "kernel32" _
      Alias "CreateProcessA" ( _
        ByVal lpApplicationName As String, _
        ByVal lpCommandLine As String, _
        lpProcessAttributes As Any, _
        lpThreadAttributes As Any, _
        ByVal bInheritHandles As Long, _
        ByVal dwCreationFlags As Long, _
        lpEnvironment As Any, _
        ByVal lpCurrentDirectory As String, _
        lpStartupInfo As STARTUPINFO, _
        lpProcessInformation As PROCESS_INFORMATION _
    ) As Long
    其中 lpApplicationName为应用程序文件名;lpCommandLine为命令行参数。结构类型PROCESS_INFORMATION和STARTUPINFO中存放应用程序运行时返回的信息。
      

  4.   

    哦,我不是要重定向到文件,而是要把输出重定向到VB的应用程序里面,这样可以在VB的窗口里面#立刻#看到本来是在DOS窗口里的东西(重定向到文件达不到这样的效果)
      

  5.   

    转自问专家:如何取得DOS程序的运行结果输出编号:QA004729
    建立日期: 2003年10月18日 最后修改日期:2003年10月18日 
    所属类别: Visual Basic - Windows APIQ zbasic: 
        请问如果要在VB中要如何实现QA004722 "如何调用DOS程序,并取得该程序的运行结果输出"的功能该如何做? A回答:     一个办法是使用Shell语句来实现重定向,例如在Windows 9x下可以使用下面的语句将运行结果输出到c:\temp\A.txt。 
        Shell "Command.com /c net use > c:\temp\A.txt" 
        另一个把是采用QA004722 "如何调用DOS程序,并取得该程序的运行结果输出"所介绍的方法。下面是VB版本的实现。例如,可以使用Text1.Text = RunCommand("ipconfig")调用ipconfig命令并将输出存入Text1文本框。 
        Option Explicit 
        Option Base 0 
        '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 retval 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 an check for success 
         ' note we use the default buffer size 
         ' this could cause problems if the process tries to write more than this buffer size 
         retval = CreatePipe(hRead, hWrite, sa, 0&) 
         If retval = 0 Then 
         Debug.Print "CreatePipe Failed" 
         RunCommand = "" 
         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 
         retval = CreateProcess(vbNullString, _ 
         CommandLine & vbNullChar, _ 
         sa, _ 
         sa, _ 
         1&, _ 
         NORMAL_PRIORITY_CLASS, _ 
         ByVal 0&, _ 
         vbNullString, _ 
         si, _ 
         pi) 
         If retval 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 
         Loop 
         
         'close the handles of the process 
         CloseHandle pi.hProcess 
         CloseHandle pi.hThread 
         Else 
         Debug.Print "CreateProcess Failed" & vbCrLf 
         End If 
         
         'close pipe handles 
         CloseHandle hRead 
         CloseHandle hWrite 
         
         'return the command line output 
         RunCommand = Replace(strResult, vbNullChar, "") 
        End Function 
         此问题由李海回答。 附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, Windows API, win32, api, windows api, gdi32, kernel。