Code: Select all
MsgBox % RunWaitOne("Code --list-extensions")
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
return exec.StdOut.ReadAll()
}
Code: Select all
MsgBox % RunWaitOne("Code --list-extensions")
RunWaitOne(command) {
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(ComSpec " /C " command)
return exec.StdOut.ReadAll()
}
Code: Select all
MsgBox, % CmdRet("cmd /c code --list-extensions")
CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0")
{
static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
, STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000
DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
VarSetCapacity(STARTUPINFO , siSize := A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
NumPut(siSize , STARTUPINFO)
NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
, "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
{
DllCall("CloseHandle", "Ptr", hPipeRead)
DllCall("CloseHandle", "Ptr", hPipeWrite)
throw Exception("CreateProcess is failed")
}
DllCall("CloseHandle", "Ptr", hPipeWrite)
VarSetCapacity(sTemp, 4096), nSize := 0
while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
( callBackFuncObj && callBackFuncObj.Call(stdOut) )
}
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
DllCall("CloseHandle", "Ptr", hPipeRead)
Return sOutput
}
Code: Select all
/*
fx:="cmd /c dir"
aa:=CmdRet(fx)
msgbox,%aa%
return
*/
runwait,%comspec% /c youtube-dl.exe https://www.youtube.com/watch?v=G6_DldYj0uQ |clip,,hide
msgbox,%clipboard%
;- this result seems same as above with clip :
aa:=CmdRet("youtube-dl.exe https://www.youtube.com/watch?v=G6_DldYj0uQ")
;fileappend,%aa%,test55.txt
msgbox,%aa%
return
/*
;- message case-1 file already exist
[youtube] G6_DldYj0uQ: Downloading webpage
[download] Ye Lai Xiang-G6_DldYj0uQ.mp4 has already been downloaded
[download] 100% of 5.74MiB
;- message case-2 streaming download
[youtube] G6_DldYj0uQ: Downloading webpage
[download] Destination: Ye Lai Xiang-G6_DldYj0uQ.mp4
[download] 0.0% of 5.74MiB at Unknown speed ETA Unknown ETA
[download] 0.1% of 5.74MiB at 198.37KiB/s ETA 00:29
[download] 0.1% of 5.74MiB at 462.87KiB/s ETA 00:12
[download] 0.3% of 5.74MiB at 991.86KiB/s ETA 00:05
[download] 0.5% of 5.74MiB at 2.00MiB/s ETA 00:02
[download] 1.1% of 5.74MiB at 4.07MiB/s ETA 00:01
[download] 2.2% of 5.74MiB at 4.01MiB/s ETA 00:01
[download] 4.3% of 5.74MiB at 5.37MiB/s ETA 00:01
[download] 8.7% of 5.74MiB at 2.13MiB/s ETA 00:02
[download] 17.4% of 5.74MiB at 2.67MiB/s ETA 00:01
[download] 34.8% of 5.74MiB at 2.72MiB/s ETA 00:01
[download] 69.7% of 5.74MiB at 2.64MiB/s ETA 00:00
[download] 100.0% of 5.74MiB at 2.59MiB/s ETA 00:00
[download] 100% of 5.74MiB in 00:02
*/
CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0")
{
static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
, STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000
DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
VarSetCapacity(STARTUPINFO , siSize := A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
NumPut(siSize , STARTUPINFO)
NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
, "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
{
DllCall("CloseHandle", "Ptr", hPipeRead)
DllCall("CloseHandle", "Ptr", hPipeWrite)
throw Exception("CreateProcess is failed")
}
DllCall("CloseHandle", "Ptr", hPipeWrite)
VarSetCapacity(sTemp, 4096), nSize := 0
while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
( callBackFuncObj && callBackFuncObj.Call(stdOut) )
}
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
DllCall("CloseHandle", "Ptr", hPipeRead)
Return sOutput
}
Code: Select all
Gui, Add, Edit, w500 h300
Gui, Show,, Output
CmdRet("youtube-dl.exe https://www.youtube.com/watch?v=G6_DldYj0uQ", Func("AddOutputInGui"))
return
AddOutputInGui(output) {
Control, EditPaste, %output%`r`n, Edit1, Output
}
Yes I understand, but I found the function still serves my purpose when removing the optional parameters and replacing
But, I also find that removing the encoding parameter and making this changeteadrinker wrote: ↑11 Dec 2020, 11:45No, all what you can remove are the callBackFuncObj := "" parameter and the line ( callBackFuncObj && callBackFuncObj.Call(stdOut) ).
Yes, I notice I get a list of Chinese characters if I try using sOutput .= stdOut := StrGet(&sTemp, nSize)teadrinker wrote: ↑11 Dec 2020, 12:22In some cases the encoding other than CP0 must be specified, so this parameter can be necessary.
how to show live in ahk-GUI / EDIT the download process 0-100% ( streaming download ) ?
Code: Select all
Gui, Add, Edit, w500 h300
Gui, Show,, Output
CmdRet("youtube-dl.exe https://www.youtube.com/watch?v=G6_DldYj0uQ", Func("AddOutputInGui"))
return
AddOutputInGui(output) {
Control, EditPaste, %output%`r`n, Edit1, Output
}
Code: Select all
;- Hide command console when using RunWaitOne() example / from user teadrinker
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=84266&sid=eff012cadb7a851b2c18f5f03b68408f
;------------------------------------------
cmdx:="dir" ;- problem filename like chinese , russian , german UMLAUT etc
;cmdx:="ping autohotkey.com -n 4" ;- also problem with german UMLAUT ä ü ö
;------------------------------------------
;============== GUI ===========
#warn
#noenv
name1:="Youtube-dl_TEST"
global name1
setworkingdir,%a_scriptdir%
Gui,1:default
Gui,1: -DPIScale
Gui,1:Color,Black,Black ; GUI black / EDIT is BLACK
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
;- maybe change fontsize
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
if (wa=3840)
Gui,1:Font,s12 cYellow,Lucida Console
;============ GUISIZEx DPIx FHD 1920*1080 is 100% ============
else if (wa=1920)
Gui,1:Font,s10 cYellow,Lucida Console
else
Gui,1:Font,s10 cYellow,Lucida Console
;=============================================================
x:=(wa*1)/xx,y:=(ha*1)/xx,w:=(wa*70)/xx,h:=(ha*60)/xx
Gui, Add, Edit, x%x% y%y% w%w% h%h%
x:=(wa*1)/xx,y:=(ha*63)/xx,w:=(wa*65)/xx,h:=(ha*6)/xx
Gui, Add, Edit, x%x% y%y% w%w% h%h% vED2,%cmdx% ;- add CMDx command above
x:=(wa*67)/xx,y:=(ha*63)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui,add,button, x%x% y%y% w%w% h%h% gA0, <START
x:=(wa*67)/xx,y:=(ha*66)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui,add,button, x%x% y%y% w%w% h%h% gClear, <CLEAR
x:=(wa*2)/xx,y:=(ha*2)/xx,w:=(wa*75)/xx,h:=(ha*75)/xx
Gui, Show,x%x% y%y% w%w% h%h%,%name1%
gosub,a0
return
;-------------------------
Guiclose:
Exitapp
;==============================
AddOutputInGui(output) {
Control, EditPaste, %output%`r`n, Edit1,%name1%
}
;-------------------------
A0:
Gui,1:submit,nohide
ed2a:="cmd /c " . ed2
CmdRet(ed2a, Func("AddOutputInGui"))
return
CLEAR:
Guicontrol,1:,ED2,
return
;=========================================================
CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0")
{
static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
, STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000
hPipeRead:=""
hPipeWrite:=""
sOutput:=""
DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
VarSetCapacity(STARTUPINFO , siSize := A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
NumPut(siSize , STARTUPINFO)
NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
, "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
{
DllCall("CloseHandle", "Ptr", hPipeRead)
DllCall("CloseHandle", "Ptr", hPipeWrite)
throw Exception("CreateProcess is failed")
}
DllCall("CloseHandle", "Ptr", hPipeWrite)
VarSetCapacity(sTemp, 4096), nSize := 0
while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
;sOutput .= stdOut := StrGet(&sTemp, nSize)
;sOutput .= stdOut := StrGet(&sTemp, nSize, "CP1250")
( callBackFuncObj && callBackFuncObj.Call(stdOut) )
}
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
DllCall("CloseHandle", "Ptr", hPipeRead)
Return sOutput
}
;=========================================================================================
Code: Select all
MsgBox, % "cp" . DllCall("GetOEMCP", "UInt")
Code: Select all
MsgBox, % "cp" . DllCall("GetOEMCP", "UInt")
Code: Select all
;- Hide command console when using RunWaitOne() example / from user teadrinker
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=84266&sid=eff012cadb7a851b2c18f5f03b68408f
;------------------------------------------
cmdx:="dir"
;cmdx:="ping autohotkey.com -n 4"
;------------------------------------------
;============== GUI ===========
#warn
#noenv
name1:="Youtube-dl_TEST"
global name1
setworkingdir,%a_scriptdir%
Gui,1:default
Gui,1: -DPIScale
Gui,1:Color,Black,Black ; GUI black / EDIT is BLACK
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
;- maybe change fontsize
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
if (wa=3840)
Gui,1:Font,s12 cYellow,Lucida Console
;============ GUISIZEx DPIx FHD 1920*1080 is 100% ============
else if (wa=1920)
Gui,1:Font,s10 cYellow,Lucida Console
else
Gui,1:Font,s10 cYellow,Lucida Console
;=============================================================
x:=(wa*1)/xx,y:=(ha*1)/xx,w:=(wa*70)/xx,h:=(ha*60)/xx
Gui, Add, Edit, x%x% y%y% w%w% h%h%
x:=(wa*1)/xx,y:=(ha*63)/xx,w:=(wa*65)/xx,h:=(ha*6)/xx
Gui, Add, Edit, x%x% y%y% w%w% h%h% vED2,%cmdx% ;- add CMDx command above
x:=(wa*67)/xx,y:=(ha*63)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui,add,button, x%x% y%y% w%w% h%h% gA0, <START
x:=(wa*67)/xx,y:=(ha*66)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui,add,button, x%x% y%y% w%w% h%h% gClear, <CLEAR
x:=(wa*2)/xx,y:=(ha*2)/xx,w:=(wa*75)/xx,h:=(ha*75)/xx
Gui, Show,x%x% y%y% w%w% h%h%,%name1%
gosub,a0
return
;-------------------------
Guiclose:
Exitapp
;==============================
AddOutputInGui(output) {
Control, EditPaste, %output%`r`n, Edit1,%name1%
}
;-------------------------
A0:
Gui,1:submit,nohide
ed2a:="cmd /c " . ed2
CmdRet(ed2a, Func("AddOutputInGui"))
;if ed2 contains youtube
; run,%fdmp4%
return
CLEAR:
Guicontrol,1:,ED2,
return
;=========================================================
CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0")
{
static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
, STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000
hPipeRead:=""
hPipeWrite:=""
sOutput:=""
CPX:="cp" . DllCall("GetOEMCP", "UInt")
DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
VarSetCapacity(STARTUPINFO , siSize := A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
NumPut(siSize , STARTUPINFO)
NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
, "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
{
DllCall("CloseHandle", "Ptr", hPipeRead)
DllCall("CloseHandle", "Ptr", hPipeWrite)
throw Exception("CreateProcess is failed")
}
DllCall("CloseHandle", "Ptr", hPipeWrite)
VarSetCapacity(sTemp, 4096), nSize := 0
while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
;sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
;sOutput .= stdOut := StrGet(&sTemp, nSize)
sOutput .= stdOut := StrGet(&sTemp, nSize, CPX)
( callBackFuncObj && callBackFuncObj.Call(stdOut) )
}
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
DllCall("CloseHandle", "Ptr", hPipeRead)
Return sOutput
}
;=========================================================================================
No idea. But you don't need to change the function, just specify encoding when you call it:
Code: Select all
CmdRet(ed2a, Func("AddOutputInGui"), "cp850")
Code: Select all
CPX:="cp" . DllCall("GetOEMCP", "UInt")
cmdx:="dir"
;cmdx:="ping autohotkey.com -n 4"
;..
Gui, Add, Edit, x%x% y%y% w%w% h%h% vED2,%cmdx% ;- add CMDx command above
;..
Gui,show
return
;-------------------------
A0:
Gui,1:submit,nohide
ed2a:="cmd /c " . ed2
CmdRet(ed2a, Func("AddOutputInGui"),CPX)
; or direct so not needed the DllCall at begin from script
;-CmdRet(ed2a, Func("AddOutputInGui"), "cp850")
return
;....
@teadrinker how to kill/close the pipe if it hangout in the while DllCall("ReadFile", for so long?teadrinker wrote: ↑10 Dec 2020, 11:42Code: Select all
MsgBox, % CmdRet("cmd /c code --list-extensions") CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0") { static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT , STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000 DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0) DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT) VarSetCapacity(STARTUPINFO , siSize := A_PtrSize*4 + 4*8 + A_PtrSize*5, 0) NumPut(siSize , STARTUPINFO) NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7) NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3) NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4) VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0) if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW , "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION) { DllCall("CloseHandle", "Ptr", hPipeRead) DllCall("CloseHandle", "Ptr", hPipeWrite) throw Exception("CreateProcess is failed") } DllCall("CloseHandle", "Ptr", hPipeWrite) VarSetCapacity(sTemp, 4096), nSize := 0 while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) { sOutput .= stdOut := StrGet(&sTemp, nSize, encoding) ( callBackFuncObj && callBackFuncObj.Call(stdOut) ) } DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION)) DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize)) DllCall("CloseHandle", "Ptr", hPipeRead) Return sOutput }