1

I have a printer shared on network, to which I am able to print from command line using

echo hello > \\PSNHP\PRN

or

copy /b hello.txt \\PSNHP\PRN

Idea from this link https://supportcommunity.zebra.com/s/article/Print-RAW-File-From-CMD?language=en_US

I like this because that way I do not need to install any driver on windows

I am trying to do the same from go

socket, err = os.OpenFile("\\\\"+comp+"\\PRN", os.O_WRONLY, os.ModeNamedPipe)
//or
socket, err = os.Open("\\\\"+comp+"\\PRN")

However, I am getting this error

open \\PSNHP\PRN: The system cannot find the file specified.

So, I am confused, is a network printer a file or not, how does the command prompt understand the stream redirection.

I think this question in c# has the same roots, I am not happy with the proposed answer, as I do not have direct network access to the printer Send text file directly to network printer

Sam Washington
  • 626
  • 4
  • 12

1 Answers1

1

I've narrowed it down with ProcMon from sysinternals, compared the differences and figured it out.

Not working
Desired Access: Generic Write, Read Attributes
Disposition:    Open
Options:    Synchronous IO Non-Alert, Non-Directory File
Attributes: R
ShareMode:  Read, Write
AllocationSize: n/a

Working
Desired Access: Generic Write, Read Attributes
Disposition:    OverwriteIf
Options:    Synchronous IO Non-Alert, Non-Directory File
Attributes: N
ShareMode:  Read
AllocationSize: 0
OpenResult: Created

Notice the overwriteif, well the sytem cannot find the file, but it could be created, and then the spooler on the other computer, will print it instead of actually creating the file.

My modified go code

socket, err := os.OpenFile("\\\\PSNHP\\PRN", os.O_WRONLY|os.O_CREATE, 0)
    fmt.Println(err)
    socket.Write([]byte("hello\n"))
    socket.Close()
Sam Washington
  • 626
  • 4
  • 12