WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Capturing STDOUT


I have a DLL that comes with all the standard assumptions (I don't have source, I can't change it, and, in fact, I'm quite happy with it in every way). But the DLL sends its output to standard output. I want to capture the output generated by the DLL, when I call it from my WinBatch program (via DllCall()). And note further that, unless/until one does a AllocateConsole() call, output sent to standard output goes to nowhere.

Notes:

  1. Limited output capture size. There is a hard upper bound that is somewhere around 55000. You can't use this to capture arbitrarily large amounts of output.

  2. This technique could be used if you need to run a DOS command and check its output. And More importantly see if it generated an error message or unexpected prompt that has to be dealt with.

  3. One of the longstanding problems is that if stdout is redirected to a file, then it gets buffered so the output doesn't come out on a timely basis as it does it when the output is to a "terminal". Also, under Windows, files get locked, so you can't really check the file's content before the redirected task exits, so that limits you. So, this method, being able to "Read it off the screen" could be real handy.
kern32 = DllLoad("kernel32")
DllCall(kern32, long:"AllocConsole")
hStdOutput = DllCall(kern32, long:"GetStdHandle", long:-11)

; Here, you call your DLL that produces output on STDOUT.  The output will appear in the allocated Console window.
Run("cmd.exe","")

len = 24000      ; Imperically determined...
buff = BinaryAlloc(len)
BinaryEodSet(buff,len)
topleft = 0      ; I think this is: X=low-word, Y=high-word
mret = BinaryAlloc(4)
res = DllCall(kern32, long:"ReadConsoleOutputCharacterA", long:hStdOutput, lpbinary:buff, long:len, long:topleft, lpbinary:mret)
str = StrTrim(BinaryPeekStr(buff,0,len))
Pause(StrCat("res = %res%, length(str) = ",StrLen(str),", mret = ",BinaryPeek4(mret,0)),StrCat("str = <",str,">"))


Article ID:   W17213
File Created: 2007:07:03:14:28:50
Last Updated: 2007:07:03:14:28:50