Zur Hauptseite ... Zum Onlinearchiv ... Zum Abonnement ... Zum Newsletter ... Zu den Tools ... Zum Impressum ... Zum Login ...

Ausgabe von Anwendungen auf der Kommandozeile in Variable umleiten

In manchen Fllen kann es interessant sein, die Ausgabe von Anweisungen, die man normalerweise auf der Kommandozeile absetzt, in Access weiterzuverwenden. Normalerweise leitet man die Ausgabe in eine Datei um und liest diese dann per VBA ein. Es geht aber auch wesentlich eleganter - und ohne Umweg ber die Festplatte.

Beispieldatenbank

Die nachfolgenden Quellcodes finden Sie im Modul mdlGetCmdOutput der Beispieldatenbank AccessSQLDotNet.mdb, die Sie unter demfolgenden Link herunterladen knnen.

 

Aus der Kommandozeile lesen

Das Einlesen des Ausgabe einer Anweisung erfordert einige API-Funktionen und Typdeklarationen. Die eigentliche Arbeit bernimmt die Funktion GetCmdOutput, die als Parameter den Ausdruck erwartet, der normalerweise im DOS-Prompt landen wrde.

Die Beschreibung der Funktion wrde den Rahmen dieses Beitrags sprengen, deshalb nur soviel: Die Anweisung wird ber die API-Funktion CreateProcess aufgerufen. Die nachfolgenden Befehle leiten die Ausgabe des Prozesses so um, dass diese mit der API-Funktion ReadFile gelesen werden kann und einige Schritte weiter von der Funktion zurckgegeben wird.

Option Compare Database

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 Sub GetStartupInfo Lib "kernel32" _

    Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)

Private Declare Function CreateProcess Lib "kernel32" _

    Alias "CreateProcessA" (ByVal lpApplicationName As _

    String, ByVal lpCommandLine As String, _

    lpProcessAttributes As Any, lpThreadAttributes As _

    Any, 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 ReadFile Lib "kernel32" (ByVal _

    hFile As Long, ByVal lpBuffer As Any, ByVal _

    nNumberOfBytesToRead As Long, lpNumberOfBytesRead As _#

    Long, ByVal lpOverlapped As Any) As Long

Private Declare Function CloseHandle Lib "kernel32" _#

    (ByVal hObject As Long) As Long

Declare Function WaitForSingleObject Lib "kernel32" _

    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) _

    As Long

Declare Function OemToChar Lib "user32" Alias "OemToCharA" _

    (ByVal lpszSrc As String, ByVal lpszdst As String) As Long

 

Private Type SECURITY_ATTRIBUTES

    nLength As Long

    lpSecurityDescriptor As Long

    bInheritHandle As Long

End Type

 

Private Type PROCESS_INFORMATION

    hProcess As Long

    hThread As Long

    dwProcessId As Long

    dwThreadId 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 Const STARTF_USESHOWWINDOW = &H1

Private Const STARTF_USESTDHANDLES = &H100

Private Const SW_HIDE = 0

 

Public Function GetCmdOutput(cmdLine As String)

    Dim pa As SECURITY_ATTRIBUTES

     Dim pra As SECURITY_ATTRIBUTES

    Dim tra As SECURITY_ATTRIBUTES

    Dim pi As PROCESS_INFORMATION

    Dim sui As STARTUPINFO

    Dim hRead As Long

    Dim hWrite As Long

    Dim bRead As Long

    Dim strBuffer As String * 1024

    Dim lngLen As Long

    Dim retVal As Long

    Dim strRetVal As String

    Dim strBufferANSI As String * 1024

    pa.nLength = Len(pa)

    pa.lpSecurityDescriptor = 0

    pa.bInheritHandle = True

    pra.nLength = Len(pra)

    tra.nLength = Len(tra)

    If CreatePipe(hRead, hWrite, pa, 0) <> 0 Then

        sui.cb = Len(sui)

        GetStartupInfo sui

        sui.hStdOutput = hWrite

        sui.hStdError = hWrite

        sui.dwFlags = STARTF_USESHOWWINDOW Or _

        STARTF_USESTDHANDLES

    sui.wShowWindow = SW_HIDE

    If CreateProcess(vbNullString, cmdLine, pra, tra, _

            True, 0, Null, vbNullString, sui, pi) <> 0 Then

            Do

                retVal = WaitForSingleObject(pi.hProcess, 0)

                DoEvents

            Loop Until retVal <> 258

            strBuffer = String$(lngLen, vbNullChar)

            retVal = ReadFile(hRead, strBuffer, 1023, _

                bRead, vbNullString)

            strBufferANSI = String$(lngLen, vbNullChar)

            strRetVal = OemToChar(strBuffer, strBuffer)

            GetCmdOutput = strBuffer

            CloseHandle hWrite

            CloseHandle hRead

        End If

    End If

End Function

© 2003-2015 André Minhorst Alle Rechte vorbehalten.