Hi,
den folgenden Code in ein Modul kopieren:
Private Declare Function ShellExecute Lib "shell32" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As _
Long) As Long
Private Declare Function GetTempFileName Lib "kernel32" Alias _
"GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, _
ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Private Declare Function GetTempPath Lib "kernel32.dll" Alias "GetTempPathA" ( _
ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As _
Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As _
Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As _
Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OemToChar Lib "user32" Alias "OemToCharA" (ByVal _
lpszSrc As String, ByVal lpszDst As String) As Long
Public Function Konsolenanwendung(Anwendung As String) As String
Dim F As Integer
Dim sTempPath As String
Dim sTempFile As String
Dim lngResult As Long
Dim sTempInhaltFile As String
' Temporäres Verzeichnis ermitteln
sTempPath = Space$(256)
lngResult = GetTempPath(Len(sTempPath), sTempPath)
sTempPath = Left$(sTempPath, lngResult)
' Temporären Dateinamen ermitteln
sTempFile = Space$(256)
Call GetTempFileName(sTempPath, "~RES", 0&, sTempFile)
sTempFile = Left$(sTempFile, InStr(sTempFile, Chr$(0)) - 1)
Dim lRetVal As Long
Dim lHandle As Long
Dim lRet As Long
lRetVal = Shell(Anwendung & " > " & Chr(34) & sTempFile & Chr(34), vbHide)
If lRetVal <> 0 Then
lHandle = OpenProcess(&H100000, 0, lRetVal)
If lHandle <> 0 Then
lRet = WaitForSingleObject(lHandle, 119000) 'Die Wartezeit in ms
CloseHandle (lHandle)
End If
End If
sTempInhaltFile = Space(FileLen(sTempFile))
Open sTempFile For Binary As 1
Get #1, 1, sTempInhaltFile
Close 1
sTempInhaltFile = DOS_To_ANSI(sTempInhaltFile)
Konsolenanwendung = sTempInhaltFile
Kill sTempFile
If lRet <> 0 Then 'Zeitüberschreitung
Konsolenanwendung = "Fehler: Zeitüberschreitung."
End If
End Function
Private Function DOS_To_ANSI(ByVal sText As String) _
As String
On Error Resume Next
Dim lResult As Long
Dim sANSI As String
sANSI = Space$(Len(sText))
lResult = OemToChar(sText, sANSI)
DOS_To_ANSI = sANSI
End Function Man kann dann die Funktion Konsolenanwendung() aufrufen, die als Parameter die Konsolenanwendung erwartet (z. b. "cmd.exe /c dir ""C:\"""). Der Befehl wird daraufhin ausgeführt und die Ausgabe in eine temporäre Datei umgeleitet. Dann wird solange gewartet, bis das Konsolenprogramm sich beendet hat (max. 119 Sekunden). Die temporäre Datei wird dann ausgelesen (und auch gleich in den Windows-Zeichensatz konvertiert) und von der Funktion als String zurückgegeben.
MfG, Programmierer |