Code: Select all
RunCmd_Stream(CmdLine, WorkingDir:="", Cp:="CP0") { ; v0.90 by SKAN on D34E/D373
Local ; @ tiny.cc/runcmd
Global A_Args
DllCall("CreatePipe", "PtrP",hPipeR:=0, "PtrP",hPipeW:=0, "Ptr",0, "UInt",0)
, DllCall("SetHandleInformation", "Ptr",hPipeW, "UInt",1, "UInt",1)
, P8 := (A_PtrSize=8), VarSetCapacity(SI, P8? 104:68,0)
, NumPut(P8? 104:68, SI), NumPut(0x100, SI, P8? 60:44,"UInt")
, NumPut(hPipeW, SI, P8? 88:60), NumPut(hPipeW, SI, P8? 96:64)
, VarSetCapacity(PI, P8? 24:16), pWorkingDir := (WorkingDir ? &WorkingDir : 0)
If not DllCall("CreateProcess", "Ptr",0, "Str",CmdLine, "Ptr",0, "UInt",0, "UInt",True
, "UInt",0x08000000 | DllCall("GetPriorityClass", "Ptr",-1,"UInt"), "UInt",0
, "Ptr",pWorkingDir, "Ptr",&SI, "Ptr",&PI )
Return Format( "{1:}", "", ErrorLevel := -1
, DllCall("CloseHandle", "Ptr",hPipeW)
, DllCall("CloseHandle", "Ptr",hPipeR) )
DllCall("CloseHandle", "Ptr",hPipeW), A_Args.RunCMD := {"PID":NumGet(PI,P8?16:8,"UInt")}
File := FileOpen(hPipeR, "h", Cp), Line := ""
While (Line := File.ReadLine())
If not RunCmd_Output(Line, A_Index)
Break
A_Args.RunCMD.PID := 0, hProcess := NumGet(PI,0), hThread := NumGet(PI,4)
, DllCall("GetExitCodeProcess", "Ptr",hProcess, "PtrP",ExitCode:=0)
, DllCall("CloseHandle", "Ptr",hProcess), DllCall("CloseHandle", "Ptr",hThread)
, DllCall("CloseHandle", "Ptr",hPipeR)
Return ExitCode
}
; Quick test follows:
#NoEnv
#Warn
#SingleInstance, Force
SetWorkingDir %A_ScriptDir%
SetBatchLines -1
Process, Priority,,High
RunCmd_Stream(A_Comspec . " /c Dir *.*", A_AhkPath . "\..\")
SoundBeep
Return ; end of auto-execute section
RunCmd_Output(Line, LineNum) {
Sleep 1
MsgBox % Line . "#" LineNum
;Return False ; Cancel
Return True ; Continue
}