hmm ... kein Wunder habe ich sowas noch nie gebraucht
Ich verwende immer eine eigene CMD.
tue mal das in ein Modul:
Private Const SW_HIDE = 0
Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const CREATE_DEFAULT_ERROR_MODE = &H4000000
Private Const CREATE_NEW_CONSOLE = &H10
Private Const NORMAL_PRIORITY_CLASS = &H20
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 Byte
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 CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As SECURITY_ATTRIBUTES, _
ByVal nSize As Long) As Long
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 lpCurrentDriectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
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, _
ByVal lpOverlapped As Long) As Long
Public Function GetProcessOutput(ByVal sExeName As String, ByVal sParameters As _
String) As String
Dim PipeRead As Long
Dim PipeWrite As Long
Dim PipeErrRead As Long
Dim PipeErrWrite As Long
Dim pInfo As PROCESS_INFORMATION
Dim pStart As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim rVal As String, rB As Long
Dim sBuffer As String, sResultString As String
sa.nLength = Len(sa)
sa.bInheritHandle = True
CreatePipe PipeRead, PipeWrite, sa, 0
CreatePipe PipeErrRead, PipeErrWrite, sa, 0
With pStart
.cb = Len(pStart)
.dwFlags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
.hStdInput = 0
.hStdOutput = PipeWrite
.hStdError = PipeErrWrite
.wShowWindow = SW_HIDE
End With
CreateProcess vbNullString, sExeName & " " & sParameters, sa, sa, True, _
CREATE_DEFAULT_ERROR_MODE Or CREATE_NEW_CONSOLE, _
NORMAL_PRIORITY_CLASS, _
vbNullString, pStart, pInfo
If pInfo.hProcess = 0 Then Exit Function
CloseHandle PipeWrite
CloseHandle PipeErrWrite
Do
sBuffer = Space(255)
rVal = ReadFile(PipeRead, ByVal sBuffer, Len(sBuffer), rB, ByVal 0)
sResultString = sResultString & Trim$(sBuffer)
Loop While rVal <> 0
If sResultString = "" Then
Do
sBuffer = Space(255)
rVal = ReadFile(PipeErrRead, ByVal sBuffer, Len(sBuffer), rB, ByVal 0)
sResultString = sResultString & Trim$(sBuffer)
Loop While rVal <> 0
End If
CloseHandle PipeRead
CloseHandle PipeErrRead
CloseHandle pInfo.hThread
CloseHandle pInfo.hProcess
GetProcessOutput = sResultString
End Und so kann man das aufrufen:
Msgbox Module1.GetProcessOutput("net", "/h")
in diesem Fall kommt der Fehlertext
und so kommt wirklich der Output:
MsgBox Module1.GetProcessOutput("net", "view") |