网上有一个利用管道技术调用DOS命令的程序,对于ping或者telnet、ipconfig等是有响应的,对于DIR等命令不行,自己下个看看。
类模块中(类模块取名为DOSOutputs)
Option Explicit
'函数CreatePipe创建一个匿名管道,返回管道输入与输出段的句柄
Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long
'用来读取由函数CretaProcessA创建的进程的管道中的数据
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 Long
lpDesktop As Long
lpTitle As Long
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 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 hHandle As Long) As Long
Private mCommand As String
Private mOutputs As String
Public Event ReceiveOutputs(CommandOutputs As String)
Public Property Let CommandLine(DOSCommand As String)
mCommand = DOSCommand
End Property
Public Property Get CommandLine() As String
CommandLine = mCommand
End Property
Public Property Get Outputs()
Outputs = mOutputs
End Property
Public Function ExecuteCommand(Optional CommandLine As String) As String
Dim proc As PROCESS_INFORMATION
Dim ret As Long
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim hReadPipe As Long
Dim hWritePipe As Long
Dim lngBytesread As Long
Dim strBuff As String * 256
'如果参数不为空则提交命令
If Len(CommandLine) > 0 Then
mCommand = CommandLine
End If
If Len(mCommand) = 0 Then
MsgBox "Command Line empty", vbCritical
Exit Function
End If
'创建管道
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
If ret = 0 Then '错误返回
MsgBox "CreatePipe failed. Error: " & Err.LastDllError, vbCritical
Exit Function
End If
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
'设置标准输出设备和出错设备to相同的管道句柄
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
'创建进程
ret& = CreateProcessA(0&, mCommand, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
If ret <> 1 Then
MsgBox "File or command not found", vbCritical
Exit Function
End If
ret = CloseHandle(hWritePipe)
mOutputs = ""
Do
ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&)
mOutputs = mOutputs & Left(strBuff, lngBytesread)
RaiseEvent ReceiveOutputs(Left(strBuff, lngBytesread))
Loop While ret <> 0
ret = CloseHandle(proc.hProcess)
ret = CloseHandle(proc.hThread)
ret = CloseHandle(hReadPipe)
ExecuteCommand = mOutputs
End Function
窗体中,两个textbox,两个按钮
Option Explicit
Private WithEvents objDOS As DOSOutputs
Private Sub cmdExecute_Click()
On Error GoTo errore
objDOS.CommandLine = txtCommand.Text
objDOS.ExecuteCommand
Exit Sub
errore:
MsgBox (Err.Description & " - " & Err.Source & " - " & CStr(Err.Number))
End Sub
Private Sub cmdExit_Click()
Set objDOS = Nothing
End
End Sub
Private Sub Form_Load()
Set objDOS = New DOSOutputs
End Sub
Private Sub objDOS_ReceiveOutputs(CommandOutputs As String)
txtOutputs.Text = txtOutputs.Text & CommandOutputs
End Sub
Private Sub txtOutputs_Change()
txtOutputs.SelStart = Len(txtOutputs.Text)
End Sub