Read stdout from already started console

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Read stdout from already started console

07 Mar 2018, 12:17

How would it be possible to read stdout from already started console window?
Tried StdOutToVar() and LibCon this far with only bad results.
majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Re: Read stdout from already started console

07 Mar 2018, 15:37

I got it to work, but the LibCon is quite limited IMO. For example:
- ReadConsoleOutput( x, y, w, h ) function leaves much to desire. My stdout does grow one row every 10 or 20 seconds and setting the h parameter to a fixed and a too low value I run the risk of not getting all stdout rows in my var. Setting it too high (9001 rows which is max buffer height) I get too many empty rows in the var, which is a time thief and rather uneffective. In essence I am only looking for the last stdout log line, but that might not be obtainable without slapping on some parsing in a later stage. So what I am looking for is a way to calculate how many rows are written to the console so far and have that variable as h parameter, so I only capture what's actually is written and not any empty lines or cutted off stdout. Might that be possible?

It's not obtainable if using:
-GetConsoleSize() - gets the bufferwidth (120) and bufferheight (9001) which is again fixed values.
-GetConsoleClientWidth() = gives the fixed width of 1137.
-GetConsoleClientHeight() = also gives a fixed value that doesn't change of 758 and that value is definitely not the amount of written stdout rows.
So how the heck is it possible to do this smarter?

Samplescript:

Code: Select all

#SingleInstance,Off
#Include LibCon.ahk ;Needed
#NoTrayIcon ;Suggested
SetBatchLines,-1 ;suggested
LibConDebug:=1 ;let the user know about errors
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process  where name = 'cam.exe' ") ; Check for trayed oscam
 pid := process.ProcessId 
AttachConsole(pid)
;msgbox % GetConsoleClientWidth() "`n" GetConsoleClientHeight()
;GetConsoleSize(bufferwidth, bufferheight)
;msgbox % bufferwidth "`n" bufferheight
;Fileappend, % ReadConsoleOutput( 0, 0, 126, 9001 ), C:\cam\LibCon.ahk-master\pre.txt


LibCon:
Spoiler
majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Re: Read stdout from already started console

08 Mar 2018, 09:33

Really...not one with a big brain :o
It comes down to this LibCon function:

Code: Select all

	;http://msdn.microsoft.com/library/ms684965
	; pseudo function to replace the Original/real function
	ReadConsoleOutput(x, y, w, h) {
		str:=""
		Loop % h
		{
			z:=(A_Index-1)+y
			Loop % w
			{
				k:=(A_Index-1)+x
				str .= ReadConsoleOutputCharacter(k,z)
			}
			if (h!=A_Index)
				str .= "`n"
		}
		return str
	}
If h=9001 how to break the loop when all stdout lines with value are read and the first empty line gets concatenated? My CMD currently contains 1364 lines. I would like only those 1364 lines and avoid getting the remaining 7637 empty lines FileAppended.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Read stdout from already started console

09 Mar 2018, 01:41

[noob]
You're 'reading' the output of the console character by character, right? So, if the end of str contains two consecutive eol- characters you've detected an empty line, right? What about to do a SubStr() on the last two characters (or compare against the previous character) to break the loop?
[/noob]
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Read stdout from already started console

09 Mar 2018, 04:26

If you want to read the text from a console window only used for sequential output, GetConsoleCursorPosition() should return the number of rows currently contained in the screen buffer in the second parameter (y).
majstang
Posts: 90
Joined: 11 Feb 2018, 03:39

Re: Read stdout from already started console

09 Mar 2018, 08:23

Thank you so much just me for your helpful reply. Thank you BoBo! GetConsoleCursorPosition() y was the answer that solved the case. Mucking around with stdout attaching already started consoles is much harder then anticipated. Every available method I found here was problematic in some extent, either outdated or I couldn't get it to work.

CMDret - utter failure
cb.exe - outdated
Console clipboard samples scripts - utter failure
exec. scripts - did not work
StdOutToVar() - could not find a method to attach to already started console
Run, %comspec% /c cam.exe >> output.txt,, HIDE - failed because my console application doesn't work very well if starting it with Comspec. Plus the output update interval is not fastest.
LibCon - Does work :thumbup:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mapcarter and 328 guests