Writing to STDOUT of your current program

Started by KeithW, September 05, 2025, 01:36:24 PM

Previous topic - Next topic

KeithW

Greetings,

I have a script I have compiled and works fine, EXCEPT I want to drop some output to STDOUT (if possible) so that if run from a CMD prompt it displays the info during execution.  I have looked thru some STDOUT stuff on the forum which does not seem to do what I want and I have looked at the docs.  I want the simple output without using windowing commands like Message() or Display()...

Is this possible?

Keith

spl

So, are you asking for STDOUT on Commandline, or captured within a GUI?
Stan - formerly stanl [ex-Pundit]

KeithW

When run from a Command Line (EXE) I want the O/P dumped out there...

spl

Probably confusing the issue, but there is cWinbatch.exe that has a cout() function to display to command-line; otherwise (and for some reason I cannot locate it) I remember a script using php that could output to commandline.
Stan - formerly stanl [ex-Pundit]

KeithW

Thanx for the info on CWINBATCH as I did not know about that at all, not that I think it will really help, BUT something to look into just the same.

I was wanting to run a compiled script from a .bat file and I wanted certain info to dump into the runstream audit without writing specifically to a file OR having windows popup. Was not sure if that was possible or not. Might just have to approach my situation a bit differently than originally planned. Not a huge deal or deal breaker if what I was trying to do, cannot be done.  Thanx.

spl

Quote from: KeithW on September 08, 2025, 10:21:48 AMand I wanted certain info to dump into the runstream audit without writing specifically to a file OR having windows popup.

This part confused me. My experience with dumping to an audit always involved a log file, or a stream to something. On other occasions where I just wanted a script to test the environment I found using BoxOpen() to display steps along the way a nice feature. If you do decide to pursue an alternative route and do not receive an acceptable answer for your initial request, I would consider BoxOpen()
Stan - formerly stanl [ex-Pundit]

td

Not sure that I understand what  you want but here is a modified version of a very old Tech DB article that work on Windows 11 in limited testing:

; Modified version of Tech DB article:
;https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/UDF~-~UDS~Library/DOS~Console~UDFs+Write~Data~to~An~Existing~Console~Window~STDOUT.txt

#DefineFunction udfAttachConsole(consoleId)
	hker = DllLoad("KERNEL32.DLL")
	DLLCall(hker, long:"AttachConsole", LONG:consoleId)
	DllFree(hker)
	return 1
#EndFunction

#DefineFunction udfGetStdHandle()
	hker = DllLoad("KERNEL32.DLL")
	ret = DLLCall(hker, long:"GetStdHandle", long:-11)
	DllFree(hker)
	return ret
#EndFunction


#DefineFunction udfWriteConsole(hStdOutput,data)
	hker = DllLoad("KERNEL32.DLL")
	linebuff = binaryalloc(strlen(data))
	mret = BinaryAlloc(4)
	binarypokestr(linebuff, 0, data)
	DLLCall(hker, long:"WriteConsoleA", long:hStdOutput, lpbinary:linebuff, long:strlen(data), lpbinary:mret, lpnull)
	BinaryFree(linebuff)
	BinaryFree(mret)
	DllFree(hker)
	return 1
#EndFunction

#DefineFunction udfFreeConsole()
	hker = DllLoad("KERNEL32.DLL")
	DLLCall(hker, long:"FreeConsole")
	DllFree(hker)
	return 1
#EndFunction

;Check if running on XP or newer
if StrReplace(WinVersion(5),'-','') < "251" then exit

consoleid = AppProcID('cmd.exe', 1) ; udfGetConsoleProcessID()
if consoleid == 0 || consoleid == ''
	 Message("Error","No existing console window")
	 exit
endif

udfAttachConsole(consoleId)

hStdOutput = udfGetStdHandle()

for i = 1 to 200
	line = strcat("Hello there ", i, @CRLF)
	udfWriteConsole(hStdOutput,line)
next

Message("STDOUT: WRITE", "COMPLETE")

udfFreeConsole()

Exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

KeithW

Basically wanted to be able to send message out to STDOUT of a compiled program without opening any kind of Window such as Display(), Message(), BoxOpen() so essentially, if you made a .bat file containing a series of operations you could get something like this...

comannd1.exe
...completed successfully
command2.exe
...encountered condition XYZ
command3.exe
...completed successfully

etc where the "..." lines were writes to STDOUT, not a file...

That was the original, sorry if I wasn't clear.

Keith

spl

No sir, your intent was not in question (at least for me), but questions about the process for achieving it are the issue. To me it seems that any message output for an exe, even if started from a batch file would be controlled by functions within the exe. You stated you saw no reason for the output to go to a file, and furthermore, rejected WB functions like BoxOpen() or Display() that would accomplish the output w/out user interaction (unlike, Message() that would.

I mentioned CWINBATCH with it's cout() function. Surprised that Tony did not chip in about that possibility. Be nice to see a CWINBATCH script with cout().
Stan - formerly stanl [ex-Pundit]

td

Quote from: KeithW on September 08, 2025, 02:54:10 PMBasically wanted to be able to send message out to STDOUT of a compiled program without opening any kind of Window such as Display(), Message(), BoxOpen() so essentially, if you made a .bat file containing a series of operations you could get something like this...

comannd1.exe
...completed successfully
command2.exe
...encountered condition XYZ
command3.exe
...completed successfully

etc where the "..." lines were writes to STDOUT, not a file...

That was the original, sorry if I wasn't clear.

Keith

The script I provided should perform what you describe when used in a compiled script. CWinBatch only runs in its own command line like window, so it may not serve your purposes.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

One caveat. The AppProcID function returns a list of process ids when more than one cmd.exe process exists. There may be a way around this, but I haven't given it enough thought to be certain. If that's an issue for you, we can try to determine a possible solution.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

KeithW


snowsnowsnow

Is this still a live issue?

I ask because I have a general solution, that I've used for many years now that I think addresses OP's problem.  But I don't know if it is still relevant.

I have a couple questions, though:
1) Do you really need it to go stdout (in the Unix and now Windows sense) or do you really just want it displayed in the console?  I.e., there is a difference between:
a) I just want it to go to the console screen - regardless of redirection or piping.
and
b) I want this to work:

C:> MyWinBatchScript > somefile
or
C:> MyWinBatchScript | more

2) I don't understand the sub-thread about multiple instances of CMD.EXE.  Where does that come into play?

SMF spam blocked by CleanTalk