AttachConsole question

Get help with using AutoHotkey and its commands and hotkeys
JBensimon
Posts: 20
Joined: 19 Nov 2017, 11:19

AttachConsole question

03 Feb 2020, 10:47

I have a question about the following excellent code posted by user art in the old forum to demonstrate the use of AttachConsole on a hidden child CMD window for later use to capture StdOut and StdErr command output (while avoiding the briefly visible window "flash" of the alternative AllocConsole followed by WinHide): what is the purpose of the DllCall("CreateFile", …) line that I commented "REMOVE?" ? The code seems to work perfectly without it.

Thanks.

JB

Code: Select all

; Attach
DetectHiddenWindows, on
Run, %comspec% /k ,,Hide UseErrorLevel, cPid
WinWait, ahk_pid %cPid%,, 10
DllCall("AttachConsole","uint",cPid)
hCon:=DllCall("CreateFile","str","CONOUT$","uint",0xC0000000,"uint",7,"uint",0,"uint",3,"uint",0,"uint",0)  ;REMOVE?

; Test
objShell := ComObjCreate("WScript.Shell")
objExec := objShell.Exec("net use")
strStdOut:=strStdErr:=""
while, !objExec.StdOut.AtEndOfStream
     strStdOut := objExec.StdOut.ReadAll()
while, !objExec.StdErr.AtEndOfStream
     strStdErr := objExec.StdErr.ReadAll()
MSGBOX,4096,, OUT:`n[%strStdOut%]`n`nERROR:`n[%strStdErr%]

; Cleanup
DllCall("CloseHandle", "uint", hCon)  ;REMOVE?
DllCall("FreeConsole")
Process, Close, %cPid%

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], JoeWinograd, kai, poetbox, TAC109 and 287 guests