2017-06-28 2 views

Répondre

0

Depuis le AHK documentation on Run. Le deuxième exemple fournit un moyen pour ce faire:

; The following can be used to run a command and retrieve its output: 
MsgBox % RunWaitOne("dir " A_ScriptDir) 

; ...or run multiple commands in one go and retrieve their output: 
MsgBox % RunWaitMany(" 
(
echo Put your commands here, 
echo each one will be run, 
echo and you'll get the output. 
)") 

RunWaitOne(command) { 
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99 
    shell := ComObjCreate("WScript.Shell") 
    ; Execute a single command via cmd.exe 
    exec := shell.Exec(ComSpec " /C " command) 
    ; Read and return the command's output 
    return exec.StdOut.ReadAll() 
} 

RunWaitMany(commands) { 
    shell := ComObjCreate("WScript.Shell") 
    ; Open cmd.exe with echoing of commands disabled 
    exec := shell.Exec(ComSpec " /Q /K echo off") 
    ; Send the commands to execute, separated by newline 
    exec.StdIn.WriteLine(commands "`nexit") ; Always exit at the end! 
    ; Read and return the output of all commands 
    return exec.StdOut.ReadAll() 
} 
0

https://gist.github.com/maz-1/768bf7938e533907d54bff276db80904

Une bibliothèque pour capturer la sortie de ligne de commande sans fenêtre cmd

StdOutStream(sCmd, Callback = "", WorkingDir=0) { ; Modified : maz-1 
    Static StrGet := "StrGet"   ; Modified : SKAN 31-Aug-2013 
             ; Thanks to : HotKeyIt   
             ; Original : Sean 20-Feb-2007 
    tcWrk := WorkingDir=0 ? "Int" : "Str" 
    DllCall("CreatePipe", UIntP,hPipeRead, UIntP,hPipeWrite, UInt,0, UInt,0) 
    DllCall("SetHandleInformation", UInt,hPipeWrite, UInt,1, UInt,1) 
    If A_PtrSize = 8 
    { 
    VarSetCapacity(STARTUPINFO, 104, 0 )  ; STARTUPINFO   ; 
    NumPut(68,   STARTUPINFO, 0)  ; cbSize 
    NumPut(0x100,  STARTUPINFO, 60)  ; dwFlags => STARTF_USESTDHANDLES = 0x100 
    NumPut(hPipeWrite, STARTUPINFO, 88)  ; hStdOutput 
    NumPut(hPipeWrite, STARTUPINFO, 96)  ; hStdError 
    VarSetCapacity(PROCESS_INFORMATION, 24) ; PROCESS_INFORMATION ; 
    } 
    Else 
    { 
    VarSetCapacity(STARTUPINFO, 68, 0 ) 
    NumPut(68,   STARTUPINFO, 0) 
    NumPut(0x100,  STARTUPINFO, 44) 
    NumPut(hPipeWrite, STARTUPINFO, 60) 
    NumPut(hPipeWrite, STARTUPINFO, 64) 
    VarSetCapacity(PROCESS_INFORMATION, 16) 
    } 

    If ! DllCall("CreateProcess", UInt,0, UInt,&sCmd, UInt,0, UInt,0 ; 
       , UInt,1, UInt,0x08000000, UInt,0, tcWrk, WorkingDir 
       , UInt,&STARTUPINFO, UInt,&PROCESS_INFORMATION) 
    Return "" 
    , DllCall("CloseHandle", UInt,hPipeWrite) 
    , DllCall("CloseHandle", UInt,hPipeRead) 
    , DllCall("SetLastError", Int,-1)  

    hProcess := NumGet(PROCESS_INFORMATION, 0)     
    hThread := NumGet(PROCESS_INFORMATION, A_PtrSize) 

    DllCall("CloseHandle", UInt,hPipeWrite) 

    AIC := (SubStr(A_AhkVersion, 1, 3) = "1.0") ; A_IsClassic 
    VarSetCapacity(Buffer, 4096, 0), nSz := 0 

    While DllCall("ReadFile", UInt,hPipeRead, UInt,&Buffer, UInt,4094, UIntP,nSz, Int,0) { 

    tOutput := (AIC && NumPut(0, Buffer, nSz, "Char") && VarSetCapacity(Buffer,-1)) 
       ? Buffer : %StrGet%(&Buffer, nSz, "CP0") ; formerly CP850, but I guess CP0 is suitable for different locales 

    Isfunc(Callback) ? %Callback%(tOutput, A_Index) : sOutput .= tOutput 

    }     

    DllCall("GetExitCodeProcess", UInt,hProcess, UIntP,ExitCode) 
    DllCall("CloseHandle", UInt,hProcess ) 
    DllCall("CloseHandle", UInt,hThread ) 
    DllCall("CloseHandle", UInt,hPipeRead) 
    DllCall("SetLastError", UInt,ExitCode ) 
    VarSetCapacity(STARTUPINFO, 0) 
    VarSetCapacity(PROCESS_INFORMATION, 0) 

Return Isfunc(Callback) ? %Callback%("", 0) : sOutput  
}