VB 创建可等待进程并获取Stdout输出的代码
2008-03-29 13:17
417 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Attribute VB_Name = "Run_Command"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Option Explicit
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Option Base 0
'Powered by barenx
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
Private Declare Function CreateProcess()Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, _
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, _
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDirectory As String, _
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Private Declare Function CloseHandle()Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Private Declare Function ReadFile()Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Private Declare Function WaitForSingleObject()Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Private Declare Function CreatePipe()Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Type STARTUPINFO
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cb As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lpReserved As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lpDesktop As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lpTitle As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwX As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwY As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwXSize As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwYSize As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwXCountChars As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwYCountChars As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwFillAttribute As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwFlags As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
wShowWindow As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cbReserved2 As Integer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lpReserved2 As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hStdInput As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hStdOutput As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hStdError As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Type
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Type PROCESS_INFORMATION
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hProcess As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hThread As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwProcessId As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dwThreadId As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Type
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Type SECURITY_ATTRIBUTES
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
nLength As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lpSecurityDescriptor As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bInheritHandle As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End Type
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const STARTF_USESTDHANDLES As Long = &H100&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const STARTF_USESHOWWINDOW As Long = &H1&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const SW_HIDE As Long = 0&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const SW_NORMAL As Long = 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Private Const INFINITE As Long = &HFFFF&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Function GetStrFromCommand()Function GetStrFromCommand(CommandLine As String) As String
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
On Error Resume Next
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim si As STARTUPINFO 'used to send info the CreateProcess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim pi As PROCESS_INFORMATION 'used to receive info about the created process
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim retval As Long 'return value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim hRead As Long 'the handle to the read end of the pipe
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim hWrite As Long 'the handle to the write end of the pipe
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim sBuffer(4096) As Byte 'the buffer to store data as we read it from the pipe
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim lgSize As Long 'returned number of bytes read by readfile
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim sa As SECURITY_ATTRIBUTES
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim strResult As String 'returned results of the command line
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
With sa
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.nLength = Len(sa)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.bInheritHandle = 1& 'inherit, needed for this to work
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.lpSecurityDescriptor = 0&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End With
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retval = CreatePipe(hRead, hWrite, sa, 0&)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
With si
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.cb = Len(si)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.wShowWindow = SW_HIDE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End With
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retval = CreateProcess(vbNullString, CommandLine & vbNullChar, sa, sa, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle hWrite
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'WaitForSingleObject pi.hProcess, INFINITE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Do While 0 <> ReadFile(hRead, sBuffer(0), 4096, lgSize, ByVal 0&)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strResult = strResult & Left$(sBuffer(), lgSize)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Erase sBuffer()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DoEvents
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Loop
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle pi.hProcess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle pi.hThread
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle hRead
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
GetStrFromCommand = StrConv(strResult, vbUnicode)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Function
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Public Function RunCommand()Function RunCommand(ByVal CommandLine As String, ByVal WaitForIt As Boolean, Optional ByVal ShowWindow As Boolean = False) As Long
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
On Error Resume Next
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim si As STARTUPINFO 'used to send info the CreateProcess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim pi As PROCESS_INFORMATION 'used to receive info about the created process
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim retval As Long 'return value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'Dim hRead As Long 'the handle to the read end of the pipe
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'Dim hWrite As Long 'the handle to the write end of the pipe
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'Dim lgSize As Long 'returned number of bytes read by readfile
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Dim sa As SECURITY_ATTRIBUTES
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'Dim strResult As String 'returned results of the command line
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
With sa
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.nLength = Len(sa)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.bInheritHandle = 1& 'inherit, needed for this to work
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.lpSecurityDescriptor = 0&
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End With
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If ShowWindow Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
With si
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.cb = Len(si)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.dwFlags = STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.wShowWindow = SW_NORMAL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'.hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End With
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
With si
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.cb = Len(si)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.dwFlags = STARTF_USESHOWWINDOW 'tell it to use (not ignore) the values below
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
.wShowWindow = SW_HIDE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
'.hStdOutput = hWrite 'pass the write end of the pipe as the processes standard output
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End With
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
retval = CreateProcess(vbNullString, CommandLine & vbNullChar, sa, sa, 1&, NORMAL_PRIORITY_CLASS, ByVal 0&, vbNullString, si, pi)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
If WaitForIt Then
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WaitForSingleObject pi.hProcess, INFINITE
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
End If
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RunCommand = pi.dwProcessId
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle pi.hProcess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CloseHandle pi.hThread
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
End Function
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
相关文章推荐
- VB获取某个进程的路径代码。
- VB 获取进程/模块信息 批量结束进程的代码
- 进程创建、等待及终止的代码实现
- 为什么fork创建子进程后,父进程中运行过的代码在子进程中不再运行了
- Java连接HBASE数据库,创建一个表,删除一张表,修改表,输出插入,修改,数据删除,数据获取,显示表信息,过滤查询,分页查询,地理hash
- vb代码:获取网卡实际MAC
- linux下进程的状态,创建,替换,等待,终止
- 创建进程并获取它的窗口句柄【详细说明】
- python 获取CPU、内存、硬盘、进程状态信息 代码
- 如何获取某个进程的主窗口以及创建进程的程序名?
- 如何获取子进程的输出
- VB 获取所有逻辑磁盘信息 代码
- proxool代码创建数据库连接与获取监控状态
- 【linux】进程创建、等待、终止
- VB6.0新手入门代码 用VB创建一个以当前年月日命名的文件夹
- 和菜鸟一起深入学习国嵌实验之进程创建,exec函数,进程等待
- 进程的创建 获取信息 进程模块信息 结束等候进程 windows工作等14.4.28
- 从子进程的标准输出获取信息
- linux下ss命令源码学习(二):linux c代码获取后台进程名称及pid
- 同步或异步调用EXE(创建子进程并重定向子进程的输入、输出)