Live-Forum - Die aktuellen Beiträge
Datum
Titel
29.03.2024 13:14:12
28.03.2024 21:12:36
28.03.2024 18:31:49
Anzeige
Archiv - Navigation
1156to1160
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Batch starten und warten

Batch starten und warten
Ralf_P
Hallo zusammen,
ich möchte eine *.bat aufrufen deren fertigstellung abwarten.
Folgenden code habe ich im archiv gefunden:

Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const WAIT_TIMEOUT = &H102&
Declare
Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare
Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long


Sub DOSShell()
WartenBisFertig ("C:\Programme\Dyn\bin\test.bat")
MsgBox "FERTIG"
End Sub
Sub WartenBisFertig(strEXE As String)
Dim ProcessID As Long
Dim hProcess As Long
Dim RetVal As Long
ProcessID = Shell(strEXE, vbHide)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)
Do
DoEvents
RetVal = WaitForSingleObject(hProcess, 50)
Loop Until RetVal  WAIT_TIMEOUT
End Sub

Mein Problem ist, dass die test.bat startet, aber die MSGBOX sofort erscheint.
Anscheinend wird auch nicht die cmd.exe überwacht, welche die test.bat abarbeitet.
Irgendwie bekomme ich das nicht zum laufen.
Könnt Ihr mir auf die Sprünge helfen?
Viele Grüße, Ralf

3
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
Auf Anwendung warten!
07.05.2010 10:32:55
Backowe
Hi Ralf,
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
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 WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes  _
As Long, _
ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As  _
Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory 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 GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
Dim ret&
' Initialize the STARTUPINFO structure:
Start.cb = Len(Start)
' Start the shelled application:
ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, Start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret&
End Function
Sub test()
ExecCmd ("notepad.exe")
MsgBox "Fertig"
End Sub

Anzeige
AW: Auf Anwendung warten!
07.05.2010 10:45:11
Ralf_P
Hallo,
vielen Dank erstmal - werde es gleich mal testen.
AW: Klappt hervorragend
07.05.2010 12:02:02
Ralf_P
Hallo,
Habe Deinen Code folgendermaßen angepasst:
Sub test()
ExecCmd ("C:\Programme\Dyn\bin\test.bat")
MsgBox "Fertig"
End Sub
Jetzt muß ich mir nur noch die Zeit nehmen, um die Zusammenhänge zu verstehen ;-)
Vielen Dank für die schnelle Hilfe.
Gruß, Ralf

300 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige