viewpoint-particle

Author Topic: Easy way to print out directly fom winbatch to standard windows printer??  (Read 397 times)

Beat

  • Newbie
  • *
  • Posts: 12
    • Camping Saland
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
si tacuisses, philosophus mansisses!! ;-)
if you had remained silent, you would have continued to be a philosopher!!
Wenn du geschwiegen hättest, wärst du ein Philosoph geblieben!!

stanl

  • Pundit
  • *****
  • Posts: 1164
Perhaps run echo [you data or variable] to printer

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
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
strStuff = "Hello world"
xx=FileOpen("LPT1","WRITE")
FileWrite(xx,strStuff)
FileClose(xx)
exit
 

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

Beat

  • Newbie
  • *
  • Posts: 12
    • Camping Saland
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
si tacuisses, philosophus mansisses!! ;-)
if you had remained silent, you would have continued to be a philosopher!!
Wenn du geschwiegen hättest, wärst du ein Philosoph geblieben!!

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
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
strStuff = "Hello world"
xx=FileOpen("LPT2","WRITE")
FileWrite(xx,strStuff)
FileClose(xx)
exit
 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
Very rough script with share creation and printer mapping added:

Code: Winbatch
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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
 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Beat

  • Newbie
  • *
  • Posts: 12
    • Camping Saland
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)
si tacuisses, philosophus mansisses!! ;-)
if you had remained silent, you would have continued to be a philosopher!!
Wenn du geschwiegen hättest, wärst du ein Philosoph geblieben!!

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Beat

  • Newbie
  • *
  • Posts: 12
    • Camping Saland
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
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....  ;)
si tacuisses, philosophus mansisses!! ;-)
if you had remained silent, you would have continued to be a philosopher!!
Wenn du geschwiegen hättest, wärst du ein Philosoph geblieben!!

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
What's the challenge of just pressing a button?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

....IFICantBYTE

  • Full Member
  • ***
  • Posts: 122
You could try this for just text:

Code: Winbatch
;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
Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
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
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
 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

  • Sr. Member
  • ****
  • Posts: 339
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.

The mind is everything; What you think, you become.

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

ChuckC

  • Full Member
  • ***
  • Posts: 247
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.

td

  • Tech Support
  • *****
  • Posts: 3453
    • WinBatch
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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

  • Sr. Member
  • ****
  • Posts: 339
Quote
just because the traffic originates within a segment doesn't make it safe
So true!!
The mind is everything; What you think, you become.