如何将打印机驱动程序转换为可以生成包含要发

How to convert a printer driver to a stand-alone console application which can generate a printer file containing the bytes to be sent to the printer?(如何将打印机驱动程序转换为可以生成包含要发送到打印机的字节的打印机文件的独立控制台应用程序?) - IT屋-程序员软件开发技术

本文介绍了如何将打印机驱动程序转换为可以生成包含要发送到打印机的字节的打印机文件的独立控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,生成某个数据文件的唯一方法是将其手动打印到 FILE:在 Windows 下并将其保存在文件中以供进一步处理.

I have a situation where the only way to generate a certain datafile is to print it manually to FILE: under Windows and save it in a file for further processing.

我真的很想有一个小的独立程序嵌入这个二进制打印机驱动程序,这样我就可以从一个批处理文件运行它并让它为我生成那个二进制文件,因为我们可以完全自动化保存文件"在 Visio 中,将其打印"并上传到最终目的地并触发远程测试".

I would really like to have a small stand-alone program which embeds this binary printer driver so I can run it from a batch file and have it generate that binary file for me, as we can then fully automate the "save file in Visio, 'print' it and upload it to the final destination and trigger a remote test".

这可以通过合适的 Windows SDK 实现吗?我是一名 Java 程序员,所以我不知道 Visual Studio 和 MSDN 的可能性——但是!- 但我会很感激指针.

Is this possible with a suitable Windows SDK? I am a Java programmer, so I do not know Visual Studio and the possibilities with MSDN - yet! - but I'd appreciate pointers.

我有该打印机驱动程序的安装文件,包括 32 位和 64 位.旧版本可能包含 16 位驱动程序.

I have the installation files for that printer driver, both 32 and 64 bit. Older versions may include a 16 bit driver.

打印到文件:"功能正是文档所推荐的.我已经使用 LPR 协议玩了一点,看看它可以做什么.我还是更喜欢调用小二进制"的方法.

The "print to FILE:" functionality is just what was recommended by the documentation. I have played a little bit with using the LPR-protocol to see what it can do. I'd still prefer the "invoke small binary" approach.

推荐答案

你制定的一般问题很难解决.大多数打印机驱动程序由一些众所周知的组件组成,例如打印监视器、打印处理器等,这些组件在 Windows 驱动程序工具包 http://msdn.microsoft.com/en-us/library/ff560885%28v=VS.85%29.aspx.几年前,我写了一个打印监视器.它在客户那里工作了很多年.所以我确切地知道我在写什么.打印监视器只不过是一个具有良好文档功能的 DLL.大多数其他打印机组件也是如此.这些 DLL 将从 Spooler 加载和调用.如果您有现代打印机驱动程序,则它没有在内核模式下运行的组件.所以可以加载构成每个打印机驱动程序的大部分DLL并调用相应的函数.

The general problem which you formulate is difficult to solve. Mostly a printer driver consists from some well known components like Print Monitor, Print Processor etc. which are well documented in Windows Driver Kit http://msdn.microsoft.com/en-us/library/ff560885%28v=VS.85%29.aspx. Some years ago I wrote a Print Monitor. It worked many years at a customer. So I know exactly what I writing about. A Print Monitor is nothing more as a DLL with well documented functions. The same is about most other printer components. Those DLLs will be loaded and called from Spooler. If you have a modern printer driver it has no components which run in kernel mode. So one can load most of DLLs from which consist every printer driver and call corresponding function.

您对使用一个 Concert 打印机驱动程序很感兴趣.所以首先应该做的是检查如何这个驱动程序是实现的.如果您发现哪个组件完成了您需要的工作,您可能能够在您的进程中加载​​此 DLL 并生成您需要的输出.您可以发布一个我可以下载此驱动程序的 URL 吗?

You are interesting for using one concert printer driver. So the first what one should do is to examine how this driver is implemented. If you find out which component do the job which you need, you will be probably able to load this DLL in your process and produce output which you need. It is possible that you post an URL where I could download this driver?

已更新:我对您的要求做了更多说明.在我看来,您可以按照打印机驱动程序开发人员建议的方式进行.如果驱动程序可以打印到本地端口文件,那么它可以在任何打印机端口打印.因此,您可以从 C:WinDDK7600.16385.1srcprintmonitorslocalmon 提供端口监视器服务器驱动程序的 src(另请参见 http://msdn.microsoft.com/en-us/library/ff556478%28v=VS.85%29.aspx, http://msdn.microsoft.com/en-us/library/ff549405%28v=VS.85%29.aspx 和 http://msdn.microsoft.com/en-us/library/ff563806%28v=VS.85%29.aspx).(我是一个windows 32/64 DLL,不是真正的驱动程序)并且做了一些小的修改.您可以将结果发送到您的应用程序,而不是将结果保存到文件中.它将 100% 工作,没有任何技巧.如果您在理解 localmon 时遇到问题,我可以给您一些提示.它真的不复杂.您需要做的主要更改是从 localmon 修改 LcmStartDocPort LcmWritePort LcmReadPort LcmEndDocPort 函数.c.将端口 DLL 与典型 DLL 区分开来的一些简单的事情是,它不是导出所有 DLL 的函数,而是仅导出一个 InitializePrintMonitor2 并带有指向所有其他函数的指针.

UPDATED: I though a little more about your requirements. It seems to me you can goes with the way suggested by developer of the printer driver. If the driver can print to a local port FILE, then it can print in any printer port. So you can give src of a Port Monitor Server driver from C:WinDDK7600.16385.1srcprintmonitorslocalmon (see also http://msdn.microsoft.com/en-us/library/ff556478%28v=VS.85%29.aspx, http://msdn.microsoft.com/en-us/library/ff549405%28v=VS.85%29.aspx and http://msdn.microsoft.com/en-us/library/ff563806%28v=VS.85%29.aspx). (I is a windows 32/64 DLL, not a real driver) and makes small modification. Instead of saving results to a file you can dispatch the results to your application. It will be work with 100% without any tricks. If you will have some problem to understand localmon I can give you some tips. It is really not complex. The main changes which you have to do is to modify LcmStartDocPort LcmWritePort LcmReadPort LcmEndDocPort functions from localmon.c. Some easy thing which is distinguish Port DLL from a typical DLL, that instead of exporting all DLL's functions it export only one InitializePrintMonitor2 with pointers to all other functions.

更新 2:使用本地端口"监视器的另一个提示.如果进入打印机配置,则选择添加端口...",选择本地端口"并单击新端口...",您可以键入任何文件名,例如C: empmy.bin".然后,您通过打印机打印的所有内容都将打印在此文件中,无需任何用户迭代.该名称可以是任何 win32 文件名(也允许使用UNC 名称或命名管道).通过这种方式,您可以在不使用 DDK 编程的情况下实现一些场景.

UPDATED 2: One more tip for usage of "Local Port" monitor. If goes in printer configuration, then choose "Add Port...", select "Local Port" and click "New Port..." you can type any file name like "C: empmy.bin". Then all what you print through a printer will be printed in this file without any user iteration. The name can be any win32 file name (UNC names or Named pipes are also allowed). With this way you can realize some scenarios without any programming with DDK.

更新 3:我从不同方面查看了打印机驱动程序,并在 DDK 中的 API 中再次查看了一次.现在我想推荐您选择最简单的方式,以及驱动程序制造商完全支持的方式.我建议如下:

UPDATED 3: I looked at the printer driver from different sides and looked one more time in the API in DDK. Now I want recommend you to choose the easiest way, and the way which will be full supported from the driver manufacturer. I suggest following:

您使用所需的驱动程序安装打印机,并选择具有固定文件名的 本地端口 作为输出端口(请参阅更新 2).我在这里将目标文件名命名为 C:TEMPOutput.afp.因此,您会收到与推荐驱动程序制造商完全相同的情况.固定文件名绝对FILE:端口相同.因此,如果您打印到您在 C:TEMP 目录中的 Output.afp 文件中收到的打印机.为了确保编写结束,您可以使用 ReadDirectoryChangesWFindNextChangeNotification/FindFirstChangeNotification 函数,其中 dwNotifyFilter 等于 FILE_NOTIFY_CHANGE_LAST_WRITE.然后您会在文件的最后写入时间后收到通知.这意味着在写入结束之后和 FileClose 之后以及缓存被充分刷新之后.所以文件 Output.afp 没有被锁定,您可以真正安全地读取结果.

You install a printer with the driver which you need and choose as the output port a Local Port with a fixed file name (see Update 2). I named here the destination filename as C:TEMPOutput.afp. So you receive exactly the same situation like recommend you driver manufacturer. Fixed file name is absolutely the same as FILE: port. So if you print to the printer you receive in Output.afp file in the C:TEMP directory. To be sure the end of writing you can use ReadDirectoryChangesW or FindNextChangeNotification / FindFirstChangeNotification functions with dwNotifyFilter equal to FILE_NOTIFY_CHANGE_LAST_WRITE. Then you receive notification after last write-time of the file. It means after the end of writing and after FileClose and after the cache is sufficiently flushed. So the file Output.afp is not locked and you can really safe read the results.

对于简单文档的打印,您可以使用 WritePrinter 功能(参见 http://msdn.microsoft.com/en-us/library/dd162959%28VS.85%29.aspx 并在文档中备注 http://msdn.microsoft.com/en-us/library/dd145226%28VS.85%29.aspx).使用位图、颜色和不同字体编写复杂文件,您必须在 Windows 中使用像这样的典型 GDI API(请参阅 http://msdn.microsoft.com/en-us/library/dd162865%28v=VS.85%29.aspx).

For printing of simple documents you can use WritePrinter function (see http://msdn.microsoft.com/en-us/library/dd162959%28VS.85%29.aspx and remark in the documentation http://msdn.microsoft.com/en-us/library/dd145226%28VS.85%29.aspx). Writing of complex files with bitmaps, color and different fonts you have to use typical GDI API like one this in Windows (see http://msdn.microsoft.com/en-us/library/dd162865%28v=VS.85%29.aspx).

这个解决方案看起来不像编写打印机驱动程序组件或模拟打印机驱动程序的假脱机环境那样非常壮观,但它会工作,会安全工作,并且会得到驱动程序制造商的全力支持.

This solution looks not very spectacular like writing a printer driver component or a simulation of spooler environment for printer driver, but it will work, will safe work and will be full supported from the driver manufacturer.

这篇关于如何将打印机驱动程序转换为可以生成包含要发送到打印机的字节的打印机文件的独立控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将打印机驱动程序转换为可以生成包含要发

基础教程推荐