How to use a WB Script as Cmdline Filter
Keywords: how to use a WB script as cmdline filter
Console functions for WinBatch ...
- only short and simple demonstration ... not fully proofed ...
- error checking fully discarded ... only for a quick preview ...
- demonstrates how to use a WB script as cmdline filter
#DefineFunction udfConAllocConsole () result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"AllocConsole") If (result==0) Then result=DllLastError() Return (result) ; Allocates a new console for the calling process. #EndFunction #DefineFunction udfConFreeConsole () result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"FreeConsole") If (result==0) Then result=DllLastError() Return (result) ; Detaches the calling process from its console. #EndFunction #DefineFunction udfConGetHandleStdIn () result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"GetStdHandle",long:-10) If (result==0) Then result=DllLastError() Return (result) ; Retrieves a handle for the standard input. #EndFunction #DefineFunction udfConGetHandleStdOut () result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"GetStdHandle",long:-11) If (result==0) Then result=DllLastError() Return (result) ; Retrieves a handle for the standard output. #EndFunction #DefineFunction udfCloseHandle (handle) result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"CloseHandle",long:handle) If (result==0) Then result=DllLastError() Return (result) #EndFunction #DefineFunction udfConSetConsoleMode (hConsoleHandle, mode) result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"SetConsoleMode",long:hConsoleHandle,long:mode) If (result==0) Then result=DllLastError() Return (result) ; Sets the input mode of a console's input buffer or the output mode of a console screen buffer. #EndFunction #DefineFunction udfWriteFile (hOutput, str) bbsize = StrLen(str) bb = BinaryAlloc(bbsize) BinaryPokestr(bb,0,str) bbcount = BinaryAlloc(4) BinaryEodset(bbcount,4) result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"WriteFile",long:hOutput,lpbinary:bb,long:bbsize,lpbinary:bbcount,lpnull) If (result==0) Then result=DllLastError() BinaryFree(bbcount) BinaryFree(bb) Return (result) #EndFunction #DefineFunction udfReadFile (hInput) bbsize = 4096 bb = BinaryAlloc(bbsize) BinaryEodset(bb,bbsize) bbcount = BinaryAlloc(4) result = DllCall(StrCat(DirWindows(1),"kernel32.dll"),long:"ReadFile",long:hInput,lpbinary:bb,long:bbsize,lpbinary:bbcount,lpnull) If (result==0) result=DllLastError() Else BinaryEodset(bbcount,4) result = BinaryPeekstr(bb,0,bbsize) EndIf BinaryFree(bb) BinaryFree(bbcount) Return (result) #EndFunction ; --- test ----------------------------------------------------------------- udfConAllocConsole () TimeDelay(1) stdin = udfConGetHandleStdIn () stdout = udfConGetHandleStdOut () udfConSetConsoleMode (stdin, 3) udfConSetConsoleMode (stdout, 1) fw = FileOpen("pipeout.txt","write") count=0 inputstr = "" While 1 inputstr = StrCat(inputstr,udfReadFile(stdin)) linestr = ItemExtract(1,inputstr,@cr) inputstr = ItemRemove(1,inputstr,@cr) inputstr = ItemRemove(1,inputstr,@lf) ; ... some line processing ... count=count+1 linestr = StrCat(StrFixleft(count,"0",5)," ",linestr,@crlf) ; write to console stdout udfWriteFile(stdout,linestr) ;write to normal diskfile FileWrite(fw,linestr) If (inputstr=="") Then Break EndWhile udfCloseHandle(stdin) udfCloseHandle(stdout) TimeDelay(1) udfConFreeConsole () FileClose(fw) ExitThe following opens a dos box and type in (example):dir /-p | "d:\programfiles\winbatch\system\winbatch.exe" "thiswbt.wbt"... Or simply create a bat file with this line copied into and envoke the batchfile on the commandline. It should work. Output from "thiswbt.wbt" goes To console screen.Running in Studio doesn't look so good, because there is no echo on screen while the line is reading from stdin. Don't worry, press Enter or ctrl-c to escape from unwanted black screen.
Instead of stdout we are able to write out to a textfile.
Got the idea?
I'd forgotten to say, that these standard names work too:
udfConAllocConsole () fr = FileOpen ("con.","read") ; the console input handle fw = FileOpen ("con.","write") ; the console output handle ;fr = FileOpen ("conin$","read") ; the "everlasting" console input handle ;fw = FileOpen ("conout$","write") ; the "everlasting" console output handle str = FileRead (fr) ; ... FileWrite (fw,str) FileClose (fr) FileClose (fw) udfConFreeConsole ()Detlev Dalitz
DD.20020219.0140
Article ID: W15316