Hide command console when using RunWaitOne() example Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Hide command console when using RunWaitOne() example

Post by fade2gray » 10 Dec 2020, 11:23

Following the example given in the documentation, this code works for me but I can't figure where to include the Hide parameter to prevent the command console from appearing.

Code: Select all

MsgBox % RunWaitOne("Code --list-extensions")

RunWaitOne(command) {
    shell := ComObjCreate("WScript.Shell")
    exec := shell.Exec(ComSpec " /C " command)
    return exec.StdOut.ReadAll()
}
Any advice please?


User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hide command console when using RunWaitOne() example

Post by fade2gray » 10 Dec 2020, 11:35

@BoBo Thanks, I'll see if I can adapt.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Hide command console when using RunWaitOne() example  Topic is solved

Post by teadrinker » 10 Dec 2020, 11:42

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
}

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Hide command console when using RunWaitOne() example

Post by garry » 11 Dec 2020, 08:33

@teadrinker thank you for cmdret example
in msgbox I see the result when process finished
a question : how to show live in ahk-GUI / EDIT the download process 0-100% ( streaming download ) ?

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
}

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Hide command console when using RunWaitOne() example

Post by teadrinker » 11 Dec 2020, 09:12

Try:

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
}

User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hide command console when using RunWaitOne() example

Post by fade2gray » 11 Dec 2020, 09:38

@BoBo
Including DllCall("AllocConsole") and WinHide % "ahk_id " DllCall("GetConsoleWindow", "ptr") in the function, greatly reduces the duration the console is displayed but doesn't fully hide it.

@teadrinker
While this does hide he console, I don't need the content of sOutput to be displayed in a message box, just the need to parse it for further processing; can the code be reduced for that purpose?

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Hide command console when using RunWaitOne() example

Post by teadrinker » 11 Dec 2020, 10:52

You can just save what CmdRet() returns in a variable.

User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hide command console when using RunWaitOne() example

Post by fade2gray » 11 Dec 2020, 11:34

teadrinker wrote:
11 Dec 2020, 10:52
You can just save what CmdRet() returns in a variable.
Yes I understand, but I found the function still serves my purpose when removing the optional parameters and replacing
( callBackFuncObj && callBackFuncObj.Call(stdOut) )
with
( "" && "".Call(stdOut) )

So, not fully understanding exactly what the code does, I just wondered if it could be shaved down any further, e.g. I could reduce the original example to a relatively short one-liner
installedExts := ComObjCreate("WScript.Shell").Exec(ComSpec " /C Code --list-extensions").StdOut.ReadAll()
although I realize this is unlikely with your function.

Apologies for not making my question clearer.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Hide command console when using RunWaitOne() example

Post by teadrinker » 11 Dec 2020, 11:45

fade2gray wrote: I just wondered if it could be shaved down any further
No, all what you can remove are the callBackFuncObj := "" parameter and the line ( callBackFuncObj && callBackFuncObj.Call(stdOut) ).

User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hide command console when using RunWaitOne() example

Post by fade2gray » 11 Dec 2020, 12:08

teadrinker wrote:
11 Dec 2020, 11:45
No, all what you can remove are the callBackFuncObj := "" parameter and the line ( callBackFuncObj && callBackFuncObj.Call(stdOut) ).
But, I also find that removing the encoding parameter and making this change
sOutput .= stdOut := StrGet(&sTemp, nSize, "CP0") ; or even providing an empty string, ""
still provides functionality.

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Hide command console when using RunWaitOne() example

Post by teadrinker » 11 Dec 2020, 12:22

In some cases the encoding other than CP0 must be specified, so this parameter can be necessary.

User avatar
fade2gray
Posts: 85
Joined: 21 Apr 2015, 12:28

Re: Hide command console when using RunWaitOne() example

Post by fade2gray » 11 Dec 2020, 12:42

teadrinker wrote:
11 Dec 2020, 12:22
In some cases the encoding other than CP0 must be specified, so this parameter can be necessary.
Yes, I notice I get a list of Chinese characters if I try using sOutput .= stdOut := StrGet(&sTemp, nSize)

Thank you.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Hide command console when using RunWaitOne() example

Post by garry » 11 Dec 2020, 12:46

@teadrinker thank you very much , your example works fine
;-- needs function cmdret and youtube-dl.exe for this example
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
 }
 

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Hide command console when using RunWaitOne() example

Post by garry » 25 Dec 2020, 06:27

@teadrinker also again maybe the same question , example with command 'dir' , how to see in edit the right characters , like chinese , german UMLAUT etc from filename

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
}
;=========================================================================================

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Hide command console when using RunWaitOne() example

Post by teadrinker » 25 Dec 2020, 08:52

Set an appropriate encoding, which cmd.exe uses, when you call CmdRet(ed2a, Func("AddOutputInGui")). By default it is "cp0": CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0"). It depends on your system language. For example, for Russian it is "cp866".
Perhaps, you can recognize it like this:

Code: Select all

MsgBox, % "cp" . DllCall("GetOEMCP", "UInt")

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Hide command console when using RunWaitOne() example

Post by garry » 25 Dec 2020, 11:09

@teadrinker , thank you it works , I get codepage CP850 ,
but I think it's not possible like in texteditor ( notepad saved UTF-8 with BOM ) to see all characters in GUI EDIT
It shows the filename with the correct german UMLAUT but not the other chinese characters like in Explorer
It's OK , when I use DOS commands I see german UMLAUT depending my codepage

Code: Select all

MsgBox, % "cp" . DllCall("GetOEMCP", "UInt")
;-- I tried this, see variable CPX

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
}
;=========================================================================================

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Hide command console when using RunWaitOne() example

Post by teadrinker » 25 Dec 2020, 11:15

garry wrote: I think it's not possible like in texteditor
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")

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Hide command console when using RunWaitOne() example

Post by garry » 25 Dec 2020, 11:30

@teadrinker , thank you , I don't change the function
used your script like this

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
;....

kauan014
Posts: 55
Joined: 18 Feb 2021, 20:03

Re: Hide command console when using RunWaitOne() example

Post by kauan014 » 14 Nov 2021, 21:15

teadrinker wrote:
10 Dec 2020, 11:42

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
}
@teadrinker how to kill/close the pipe if it hangout in the while DllCall("ReadFile", for so long?

Post Reply

Return to “Ask for Help (v1)”