RunCMD(CmdLine, WorkingDir:="", Codepage:="CP0", Fn:="RunCMD_Output") { ; RunCMD v0.94 Local ; RunCMD v0.94 by SKAN on D34E/D37C @ autohotkey.com/boards/viewtopic.php?t=74647 Global RunCMD_PID Fn := IsFunc(Fn) ? Func(Fn) : 0 , DllCall("CreatePipe", "PtrP",hPipeR:=0, "PtrP",hPipeW:=0, "Ptr",0, "Int",0) , DllCall("SetHandleInformation", "Ptr",hPipeW, "Int",1, "Int",1) , DllCall("SetNamedPipeHandleState","Ptr",hPipeR, "UIntP",PIPE_NOWAIT:=1, "Ptr",0, "Ptr",0) , P8 := (A_PtrSize=8) , SI:=BufferAlloc(P8?104:68,0) ; STARTUPINFO structure , NumPut("UPtr",P8?104:68,SI) ; size of STARTUPINFO , NumPut("UInt",STARTF_USESTDHANDLES:=0x100,SI,P8?60:44) ; dwFlags , NumPut("UPtr",hPipeW,SI,P8?88:60) ; hStdOutput , NumPut("UPtr",hPipeW,SI,P8?96:64) ; hStdError , PI:=BufferAlloc(P8?24:16) ; PROCESS_INFORMATION structure If not DllCall("CreateProcess", "Ptr",0, "Str",CmdLine, "Ptr",0, "Int",0, "Int",True ,"Int",0x08000000 | DllCall("GetPriorityClass", "Ptr",-1, "UInt"), "Int",0 ,"Ptr",WorkingDir ? WorkingDir.Ptr : 0, "Ptr",SI.Ptr, "Ptr",PI.Ptr) Return Format("{1:}", "", ErrorLevel := -1 ,DllCall("CloseHandle", "Ptr",hPipeW), DllCall("CloseHandle", "Ptr",hPipeR)) DllCall("CloseHandle", "Ptr",hPipeW) , RunCMD_PID := NumGet(PI,P8?16:8,"UInt") , File := FileOpen(hPipeR, "h", Codepage) , LineNum := 1, sOutput := "" While (RunCMD_PID + DllCall("Sleep", "Int",0)) and DllCall("PeekNamedPipe", "Ptr",hPipeR, "Ptr",0, "Int",0, "Ptr",0, "Ptr",0, "Ptr",0) While RunCMD_PID and (Line := File.ReadLine()) sOutput .= Fn ? Fn.Call(Line, LineNum++) : Line "`n" RunCMD_PID := 0 , hProcess := NumGet(PI,0,"UPtr") , hThread := NumGet(PI,A_PtrSize,"UPtr") , DllCall("GetExitCodeProcess", "Ptr",hProcess, "PtrP",ExitCode:=0) , DllCall("CloseHandle", "Ptr",hProcess) , DllCall("CloseHandle", "Ptr",hThread) , DllCall("CloseHandle", "Ptr",hPipeR) , ErrorLevel := ExitCode Return sOutput }