WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: Beat on May 22, 2020, 09:05:06 AM

Title: Easy way to print out directly fom winbatch to standard windows printer??
Post by: Beat on May 22, 2020, 09:05:06 AM
Hello all,

how is the most easily wey to print out something form Winbatch without create a file and also without use a extern programme like "editor" :-\

In fact I creat a "programme" which can crypt and uncryt passwords, however, some time I like to send this uncryted words to the standad windows printer.It are nomal ASCII or ANSI strings no graphics or pictures, but I don't want to save the information on the disc.
May be I have to write them in binary buffer with all CR and LF and also FF as final, no problem but I don't want to have a "phisical file" on the disc.

Any idea? :)

thanking you in anticipationand best regards from good old SwitzerlandBeat
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: stanl on May 22, 2020, 09:47:07 AM
Perhaps run echo [you data or variable] to printer
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on May 22, 2020, 10:32:52 AM
If you can map your network printer to an LPT port like LPT1 perhaps using the Net use command or the Network extender, this might work:

Code (winbatch) Select
strStuff = "Hello world"
xx=FileOpen("LPT1","WRITE")
FileWrite(xx,strStuff)
FileClose(xx)
exit


Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: Beat on May 23, 2020, 09:41:17 AM
UPPS it seems to be not easy  :-[

@ td
I don't find a way to send use my "local" printer it is connected to a IP-port (see in the attachement).
May be it is a way with to send directly to IP address.... will see
best regards from rainy SwitzerlandBeat
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on May 23, 2020, 01:52:27 PM
Happy reading:

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+Tutorials+Printing.txt (https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+Tutorials+Printing.txt)
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on May 23, 2020, 02:34:10 PM
For what it is worth, I was able to get a TCP/IP printer to work by first creating a share on my workstation for a TCP/IP printer available on my network.  I then used the above mentioned "net use" command to map the LPT2 port to the share and then executing the following script:

Code (winbatch) Select
strStuff = "Hello world"
xx=FileOpen("LPT2","WRITE")
FileWrite(xx,strStuff)
FileClose(xx)
exit
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on May 23, 2020, 03:29:19 PM
Very rough script with share creation and printer mapping added:

Code (winbatch) Select
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; No error handling and assumes printer has not been shared or
;; map to printer port.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Load Appropriate Extender
AddExtender('wwwnt34i.dll',0,'wwwnt64i.dll')
;; Use your printers device name in the second parameter
wntShareAdd('','Dell Printer E310dw XPS', 'DellLaser3', 1, -1)
;; Substitue your computer's NetBios name for 'YourComputerNameHere'.
wntAddPrinter(@Default,@Default,"\\YourComputerNameHere\DellLaser3","LPT2",@FALSE)

strStuff = "Hello world"
xx=FileOpen("LPT2","WRITE")
FileWrite(xx,strStuff)
FileClose(xx)
exit
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: Beat on May 24, 2020, 02:03:52 AM
Thanks for help!@td, IT WORKS  8) , but it was not easy,
I have to create a share, to connect LPT2 to the share and afte useing to remove LPT2 and the share but it WORKS perfect.
Thanks and enjoy WE
Regarts Beat

;***************************************************************************
;**   Print dirctly fron WinBatch
;***************************************************************************
AddExtender("WWPRT44i.DLL")
AddExtender("WWWNT34i.DLL",0,"WWWNT64i.DLL")
DefPrt = pGetDefPrtInf(1)
Share = "PRT$"
LocalShare = StrCat("\\",ComputerNameGet(0),"\",Share)
PrtPort = "LPT2"
;***************************************************************************
; create local share an connect share to LPR2 to use default printer as output over LPT2 port
WntShareAdd("",DefPrt,Share, 1, -1)
WntAddPrinter(@Default,@Default,LocalShare,PrtPort,@FALSE)
;***************************************************************************
OpenHandle=FileOpen(PrtPort,"WRITE")
FileWrite(OpenHandle,StrCat("Hello world",@CRLF,"It works"))
FileClose(OpenHandle)
;***************************************************************************
; remove connection an share
WntCancelCon(PrtPort,@TRUE,@TRUE)
WntShareDel("",DefPrt,1)
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on May 24, 2020, 04:14:45 PM
Not sure what you mean by "easy".  I guess I wouldn't consider typing a dozen or so lines of script hard work.  Figuring out what to type I suppose could be considered hard work.  It may be a bit twisted but doing a little searching of available resources to figure that out is more like fun to me.
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: Beat on May 25, 2020, 03:11:55 AM
Your are right it is easy to script, if you see the way how it works.
Maybe I don't feel easy, because I don't see how to do, till you give a helpfull hand... after then it was easy.
As you can see my solution should work on every PC.

Code (winbatch) Select
AddExtender("WWPRT44i.DLL")
AddExtender("WWWNT34i.DLL",0,"WWWNT64i.DLL")
DefPrt = pGetDefPrtInf(1)
Share = "PRT$"
LocalShare = StrCat("\\",ComputerNameGet(0),"\",Share)
PrtPort = "LPT2"
;***************************************************************************
; create local share an connect share to LPR2 to use default printer as output over LPT2 port
WntShareAdd("",DefPrt,Share, 1, -1)
WntAddPrinter(@Default,@Default,LocalShare,PrtPort,@FALSE)
;***************************************************************************
OpenHandle=FileOpen(PrtPort,"WRITE")
FileWrite(OpenHandle,StrCat("Hello world",@CRLF,"It works"))
FileClose(OpenHandle)
;***************************************************************************
; remove connection an share
WntCancelCon(PrtPort,@TRUE,@TRUE)
WntShareDel("",DefPrt,1)

And by the way, it is  much easyer to push a button --> and then it print....  ;)
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on May 25, 2020, 08:55:13 AM
What's the challenge of just pressing a button?
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: ....IFICantBYTE on June 02, 2020, 06:06:23 AM
You could try this for just text:

Code (winbatch) Select
;Example of how to print pure TEXT directly to a networked printer via a TCP/IP address and port - no print driver, LPT capture or windows printer ports need to be installed.
;Relies on the printer simply printing any text sent to it with its default font and paper settings for raw text. Most printers will do this.
;Cobbled together from other scripts and info found on WB and StackOverflow.                                                                                   ....IFICantBYTE
;=============================================================================================================================================================================

;nPort=80
nPort = 9100 ;most printers will accept text on port 9100 (works on my HP just fine)
nWait=1000 ;milliseconds
PrinterAddress = AskLine("Printer Address","Enter a valid printer's network address or DNS name." ,"192.168.1.20")
TextToPrint = "Hello World!":@CRLF:"You can concatenate a CRLF and print multiple lines too."

;ObjectClrOption("useany", "System")
ObjectClrOption ("use","System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

TCP = ObjectClrNew( 'System.Net.Sockets.TcpClient' )
Connect = TCP.Connect(PrinterAddress, nPort)
NetStream = TCP.GetStream()

StreamWriter = ObjectClrNew('System.IO.StreamWriter',NetStream)

;TestFile = FileGet("C:\Test.txt") ;a text file could be printed instead
;StreamWriter.Write(TestFile) ;send the text file to the printer

StreamWriter.WriteLine(TextToPrint);This writes a line of text to the printer

StreamWriter.Flush()
NetStream.Close()
TCP.Close()
TCP.Dispose()

Exit

Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on June 02, 2020, 07:17:16 AM
Good suggestion. Wish I had thought of that. (Would imagine the OP  wishes I had thought of that too.) Your script works with my DEL laser printer as well.  You can also use the WinSock extender:

Code (winbatch) Select
AddExtender("WWWSK44I.DLL",0,"WWWSK64I.DLL")

socket = sOpen ( )
If !socket then goto errorlabel

;; My Dell's IP address.
status = sConnect( socket,'192.168.0.104', "9100" )
if !status then goto errorlabel
status = sSendLine(socket, "Print this line")
if !status then goto errorlabel
sClose(socket)
exit

:errorlabel
if socket then sClose(socket)
error = wxGetLastErr()
ErrorText = wxGetErrDesc(err)
Message("Winsock Error ":error,ErrorText)
exit
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on June 02, 2020, 01:48:35 PM
Out of an abundance of caution should mention that Port 9100 or "Appsocket" is considered a bit of a security risk so not all printers/systems/networks support it.
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: kdmoyers on June 04, 2020, 10:31:05 AM
9100 probably should not be routed through any well configured firewall. 
But within a network segment, it works on many printers out-of-the-box.

Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on June 04, 2020, 01:25:53 PM
There are several ways to bock a port within a network segment and just because the traffic originates within a segment doesn't make it safe either. Also, not all network printers are configured to listen on the port.  I have two ethernet connected printers in my office and one happily prints data sent to port 9100 and the other completely ignores the port.
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: ChuckC on June 04, 2020, 05:26:48 PM
Just for giggles & grins... AFAIK, the usage of TCP port 9100 for printing purposes goes back to the HP JetDirect network printer interface, both the internal card and the external unit with either 1 or 3 parallel ports on it.  If you had the 3 port model, ports 9101 and 9102 were used to direct your output to the LPT2 and LPT3 devices on it instead of the LPT1 device on port 9100.

Back in the day, I was configuring an unholy mix of OpenVMS, Unix, Windows NT Advanced Server v3.1 and NetWare v3 servers to all send print jobs out to those little network beasties.
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: td on June 05, 2020, 09:01:54 AM
No surprise that my recalcitrant printer doesn't like port 9101 or 9102 either. I can still remember (just barely) the old external printer NICs.
Title: Re: Easy way to print out directly fom winbatch to standard windows printer??
Post by: kdmoyers on June 07, 2020, 09:55:33 AM
Quotejust because the traffic originates within a segment doesn't make it safe
So true!!