如何截获执行命令行的输出

原创 2002年03月14日 09:52:00
Option Explicit
Private Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Type SECURITY_ATTRIBUTES
 nLength As Long
 lpSecurityDescriptor As Long
 bInheritHandle As Long
End Type
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 Declare Function CreateProcessAsUser Lib "advapi32.dll" Alias "CreateProcessAsUserA" (ByVal hToken As Long, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As SECURITY_ATTRIBUTES, ByVal lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As String, ByVal lpCurrentDirectory As String, ByVal lpStartupInfo As STARTUPINFO, ByVal lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const STARTF_USESTDHANDLES = &H100
Private Const STARTF_USESHOWWINDOW = &H1
Private Function ExecuteCommandLineOutput(CommandLine As String, Optional BufferSize As Long = 256, Optional TimeOut As Long) As String
 Dim Proc As PROCESS_INFORMATION
 Dim Start As STARTUPINFO
 Dim SA As SECURITY_ATTRIBUTES
 Dim hReadPipe As Long
 Dim hWritePipe As Long
 Dim lBytesRead As Long
 Dim sBuffer As String
 If VBA.Len(CommandLine) > 0 Then
  SA.nLength = Len(SA)
  'SA.nLength = vba.Len(sa)
  SA.bInheritHandle = 1&
  SA.lpSecurityDescriptor = 0&
  If CreatePipe(hReadPipe, hWritePipe, SA, 0) > 0 Then
   Start.cb = Len(Start)
   Start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
   Start.hStdOutput = hWritePipe
   Start.hStdError = hWritePipe
   If CreateProcessA(0&, CommandLine, SA, SA, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, Start, Proc) = 1 Then
    CloseHandle hWritePipe
    sBuffer = VBA.String(BufferSize, VBA.Chr(0))
    If TimeOut > 0 Then
     Dim BeginTime As Date
     BeginTime = VBA.Now
    End If
    Do Until ReadFile(hReadPipe, sBuffer, BufferSize, lBytesRead, 0&) = 0
     DoEvents
     If TimeOut > 0 Then
      If VBA.DateDiff("s", BeginTime, VBA.Now) > TimeOut Then
       ExecuteCommandLineOutput = "Timeout"
       Exit Do
      End If
     End If
     ExecuteCommandLineOutput = ExecuteCommandLineOutput & VBA.Left(sBuffer, lBytesRead)
    Loop
    CloseHandle Proc.hProcess
    CloseHandle Proc.hThread
    CloseHandle hReadPipe
   Else
    ExecuteCommandLineOutput = "File or command not found"
   End If
  Else
   ExecuteCommandLineOutput = "CreatePipe failed. Error: " & Err.LastDllError & "."
  End If
 End If
End Function
Private Sub Command1_Click() '测试
 'VBA.MsgBox ExecuteCommandLineOutput("ping www.sina.com.cn")
 VBA.MsgBox ExecuteCommandLineOutput("ping www.xxxx.com.cn", , 2)
End Sub

php边执行边输出资料整理

//ignore_user_abort(true);//是否忽略浏览器的断开而继续执行脚本 header( 'Content-Type: text/html;charset=utf-8' ); ech...
  • u010324201
  • u010324201
  • 2016年03月29日 09:25
  • 331

C# 编写命令行交互工具——实时输出_获取执行结果

我们在写程序的时候通常会用到命令行工具。 如Ping 某个网段,写个注册表,启动项,或者感谢其他坏事。 在网上查了一下,多数都说用C# 做命令行交互需要做很多很多的逻辑处理。那么今天博主也来写一个简单...
  • u013151336
  • u013151336
  • 2016年05月03日 08:22
  • 4639

Mysql格式化输出

Mysql格式化输出
  • qq_36411874
  • qq_36411874
  • 2017年03月18日 10:03
  • 203

命令行日志输出

在linux中运行命令行时,会输出一些日志信息,特别典型的是启用WebLogic命令时,输出一些信息,当启用demon模式运行时,又想收集这些信息咋办? 解决的办法就是使用输出重定向,如下面的命令: ...
  • hongweigg
  • hongweigg
  • 2014年06月18日 13:56
  • 12932

windows7 命令行下执行java 命令乱码

应该是字符集的问题。方法3 最好使!其他的作参考,也许每个人的机子的当前状况跟我不一样。通过谷歌百度进行查询,解决办法主要都是修改字符集。1 .命令行窗口修改默认代码页命令行窗口上邮件选择“默认值”,...
  • lengqi0101
  • lengqi0101
  • 2016年07月18日 11:44
  • 2156

linux下命令执行结果作为其他命令输入参数

1. 通过ps命令获取对应程序的pid 比如这里要获取redis-server这个进程的pid # ps -ef | grep redis-server root 60415 1...
  • a515983690
  • a515983690
  • 2016年06月01日 10:56
  • 6311

C#中调用CMD命令行截取输出流的范例

本文实现了通过调用windows下的CMD,将cmd的输出流截取的代码片段,参考了其他几位网友的代码,如有冒犯,请多多谅解。如果需要使用管理员模式下的cmd,请参考我上一篇文章C#如何调用以管理员身份...
  • qq_17351077
  • qq_17351077
  • 2017年02月14日 17:05
  • 2091

C#获取命令行输出内容

获取命令行输出内容的方式有传统和异步两种方式。 传统方式: public static void RunExe(string exePath, string arguments, out str...
  • u012581999
  • u012581999
  • 2016年10月14日 14:44
  • 1996

yii2.0文件如何在php命令行中运行

有时候有些功能需要做到计划任务中去,因此就需要yii能用php命令去执行。yii2.0是支持php命令行运行的,操作方法如下: 在yii根目录下面有个“yii”文件,这个文件就是入口。然后“co...
  • qq_34625397
  • qq_34625397
  • 2017年04月18日 18:44
  • 1134

iOS命令行执行instruments

instruments -w {deviceId} -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents...
  • WHACKW
  • WHACKW
  • 2015年05月19日 15:27
  • 1698
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何截获执行命令行的输出
举报原因:
原因补充:

(最多只允许输入30个字)