使用GO语言打印到共享网络打印机。

huangapple go评论91阅读模式
英文:

Print to a shared network printer using GO

问题

我有一个在网络上共享的打印机,我可以使用命令行打印到它上面,例如:

echo hello > \\PSNHP\PRN

或者

copy /b hello.txt \\PSNHP\PRN

我喜欢这种方式,因为这样我不需要在Windows上安装任何驱动程序。

我正在尝试用Go语言做同样的事情:

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

然而,我遇到了这个错误:

open \\PSNHP\PRN: 系统找不到指定的文件。

所以,我感到困惑,网络打印机是一个文件吗?命令提示符如何理解流重定向?

我认为这个C#问题有相同的根源,但我对提出的答案不满意,因为我没有直接访问网络打印机的权限:

https://stackoverflow.com/questions/18704839/send-text-file-directly-to-network-printer

英文:

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
https://stackoverflow.com/questions/18704839/send-text-file-directly-to-network-printer

答案1

得分: 1

我用来自sysinternals的ProcMon进行了缩小范围的分析,比较了差异并找到了问题所在。

不工作的情况:
期望的访问权限:通用写入,读取属性
处理方式:打开
选项:同步IO非警报,非目录文件
属性:只读
共享模式:读取、写入
分配大小:n/a

工作的情况:
期望的访问权限:通用写入,读取属性
处理方式:如果存在则覆盖
选项:同步IO非警报,非目录文件
属性:无
共享模式:读取
分配大小:0
打开结果:已创建

请注意"overwriteif",系统无法找到文件,但可以创建文件,然后在另一台计算机上的打印机池将打印该文件而不是实际创建文件。

我修改后的Go代码如下:

socket, err := os.OpenFile("\\\\PSNHP\\PRN", os.O_WRONLY|os.O_CREATE, 0)
fmt.Println(err)
socket.Write([]byte("hello\n"))
socket.Close()
英文:

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()

huangapple
  • 本文由 发表于 2022年10月24日 04:43:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/74174636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定