在 .NET 中编写虚拟打印机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/257768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a Virtual Printer in .NET
提问by ctrlalt313373
I'm looking to create a virtual printer that passes data to my .NET application. I want to then create an installer that installs both the printer and the .NET application. It would we really nice to be able to write it all in C#, but I have a feeling that this will require a printer driver to be written is unmanaged code. Does anyone know of a fairly clean tutorial or example of how to do this?
我希望创建一个将数据传递到我的 .NET 应用程序的虚拟打印机。然后我想创建一个安装程序来安装打印机和 .NET 应用程序。能够用 C# 编写所有内容真是太好了,但我有一种感觉,这需要将打印机驱动程序编写为非托管代码。有谁知道一个相当干净的教程或如何做到这一点的例子?
回答by Peter Ang
Visit http://www.printerplusplus.com. It is open source .NET virtual printer. It gives you an installer and a .NET class for writing code to process your "printer data".
访问http://www.printerplusplus.com。它是开源的 .NET 虚拟打印机。它为您提供了一个安装程序和一个 .NET 类,用于编写处理“打印机数据”的代码。
回答by Ogglas
Did exactly what you are asking using the Github project: Microsoft/Windows-driver-samples/print/XPSDrvSmpl
使用 Github 项目完成了您的要求:Microsoft/Windows-driver-samples/print/XPSDrvSmpl
https://github.com/Microsoft/Windows-driver-samples/tree/master/print/XPSDrvSmpl
https://github.com/Microsoft/Windows-driver-samples/tree/master/print/XPSDrvSmpl
Installer: http://wixtoolset.org/
安装程序:http: //wixtoolset.org/
Application: Listen to internal port
应用:监听内部端口
Flow: Install printer and application from a single installer. User prints something with your driver while the application listens to the internal port. When data is sent the application picks it up. This is for XPS, can be converted to PDF, but the flow is similar no matter what you are printing. If you need anything else check out Microsoft/Windows-driver-samples/print/ on GitHub or other sources specific to your needs.
流程:从单个安装程序安装打印机和应用程序。当应用程序侦听内部端口时,用户会使用您的驱动程序打印一些内容。发送数据时,应用程序会接收它。这是针对XPS的,可以转换成PDF,但是不管你打印什么,流程都是相似的。如果您需要其他任何内容,请查看 GitHub 上的 Microsoft/Windows-driver-samples/print/ 或其他特定于您需要的资源。
Update:
更新:
A lot of questions about how to get the driver working so here is a quick example:
很多关于如何让驱动程序工作的问题,所以这里是一个简单的例子:
Start by downloading Windows Driver Kit (WDK) if you do not have it installed already. When installing choose to add the extension for Visual Studio as well in the final step. In your Visual Studio 2017 Install you need to have Desktop development with C++to have the right SDKs. If you don't have it see the anser below on how to add it.
如果您尚未安装 Windows Driver Kit (WDK),请先下载它。安装时选择在最后一步添加 Visual Studio 扩展。在您的 Visual Studio 2017 安装中,您需要Desktop development with C++拥有正确的 SDK。如果您没有它,请参阅下面有关如何添加它的 anser。
How do I add features to Visual Studio 2017?
https://developer.microsoft.com/en-us/windows/hardware/windows-driver-kit
https://developer.microsoft.com/en-us/windows/hardware/windows-driver-kit
Then download the .zipfile for Windows-driver-samples master:
然后下载.zipWindows-driver-samples master的文件:
https://github.com/Microsoft/Windows-driver-samples/archive/master.zip
https://github.com/Microsoft/Windows-driver-samples/archive/master.zip
Navigate to the folder <UnzipFolder>\print\XPSDrvSmpland open XPSDrvSmpl.slnin Visual Studio.
导航到该文件夹<UnzipFolder>\print\XPSDrvSmpl并XPSDrvSmpl.sln在 Visual Studio 中打开。
Then follow Microsofts own guide on GitHub. As you can see it is for Visual Studio 2015 but I think it is the same for 2017 (not tested yet):
然后在 GitHub 上遵循微软自己的指南。如您所见,它适用于 Visual Studio 2015,但我认为它与 2017 年相同(尚未测试):
To build a driver solution using Windows Driver Kit (WDK) 10 and Visual Studio 2015, perform the following steps.
要使用 Windows Driver Kit (WDK) 10 和 Visual Studio 2015 构建驱动程序解决方案,请执行以下步骤。
- Open the solution file in Visual Studio 2015.
- Add all non-binary files (usually located in the \install directory of the sample) to the Package project: a. In the Solution Explorer, right click Driver Files b. Select Add, then click Existing Item c. Navigate to the location to which you downloaded the sample, and select all the files in the install directory, or the equivalent set of non-binary files such as INFs, INIs, GPD, PPD files, etc. d. Click Add
- Configure these files to be added into the driver package: a. In the Solution Explorer, right click on the solution and choose Add > New Project. Choose Driver Install Package under Visual C++/Windows Driver/Package. b. In the Solution Explorer, right click the Package project and select Properties. c. In the left pane, click Configuration Properties > Driver Install > Package Files. d. In the right pane, use the ellipsis button (...) to browse to the set of files that needs to be added to the driver package. All the data files that you added in Step 2-c, except the INF file, should be added. This configuration is per-architecture, so this configuration must be repeated for each architecture that will be built. e. Click OK.
- Open the INF file and edit it to match the built output. a. Open the INF file. b. In the Version section, add a reference to a catalog file like this: CatalogFile=XpsDrvSmpl.cat. c. In the SourceDisksFiles section, change the location of the DLL files you are building, to =1. This indicates that there is no architecture specific directory in this driver. If you ship multiple architectures simultaneously, you will need to collate the driver INF manually.
- 在 Visual Studio 2015 中打开解决方案文件。
- 将所有非二进制文件(通常位于示例的 \install 目录中)添加到 Package 项目中:在解决方案资源管理器中,右键单击驱动程序文件 b。选择添加,然后单击现有项目 c。导航到您下载示例的位置,然后选择安装目录中的所有文件,或等效的非二进制文件集,例如 INF、INI、GPD、PPD 文件等。 d.点击添加
- 配置要添加到驱动程序包中的这些文件:在解决方案资源管理器中,右键单击解决方案并选择添加 > 新建项目。在 Visual C++/Windows Driver/Package 下选择 Driver Install Package。湾 在解决方案资源管理器中,右键单击包项目并选择属性。C。在左窗格中,单击配置属性 > 驱动程序安装 > 包文件。d. 在右窗格中,使用省略号按钮 (...) 浏览到需要添加到驱动程序包的文件集。您在步骤 2-c 中添加的所有数据文件(INF 文件除外)都应添加。此配置是针对每个架构的,因此必须为将要构建的每个架构重复此配置。e. 单击确定。
- 打开 INF 文件并对其进行编辑以匹配构建的输出。一种。打开 INF 文件。湾 在版本部分,添加对目录文件的引用,如下所示:CatalogFile=XpsDrvSmpl.cat。C。在 SourceDisksFiles 部分中,将您正在构建的 DLL 文件的位置更改为 =1。这表明此驱动程序中没有特定于体系结构的目录。如果您同时发布多个架构,则需要手动整理驱动程序 INF。
At this point, Visual Studio 2015 will be able to build a driver package and output the files to disk. In order to configure driver signing and deployment, see Developing, Testing, and Deploying Drivers.
此时,Visual Studio 2015 将能够构建驱动程序包并将文件输出到磁盘。要配置驱动程序签名和部署,请参阅开发、测试和部署驱动程序。
https://github.com/Microsoft/Windows-driver-samples/tree/master/print/XPSDrvSmpl#build-the-sample
https://github.com/Microsoft/Windows-driver-samples/tree/master/print/XPSDrvSmpl#build-the-sample
回答by CMS
I think you will have to do a lotof WinAPI wrapping. Start researching on Windows Driver Development Kitto find the things you have to do.
我认为你将不得不做很多WinAPI 包装。开始研究Windows 驱动程序开发工具包以找到您必须做的事情。
I also found this commercial Printer Driver Resource Toolkitfor .NET...
我还发现了这个用于 .NET 的商业打印机驱动程序资源工具包...
回答by Douglas Anderson
You could simply have your app expose itself like an LPD type printer or monitor port 9100. You could then install any print driver you like, and point it a your app.
您可以简单地让您的应用程序像 LPD 类型打印机或监视器端口 9100 一样公开自己。然后您可以安装您喜欢的任何打印驱动程序,并将其指向您的应用程序。
回答by dolzenko
Was looking for an answer for similar question, and found this link through Wikipedia - http://www.colorpilot.com/emfprinterpilot.html(allows to create Virtual Printers in different languages)
正在寻找类似问题的答案,并通过维基百科找到了此链接 - http://www.colorpilot.com/emfprinterpilot.html(允许创建不同语言的虚拟打印机)
回答by faulty
Why not consider using an alternative approach. Like using an intermediate ready made printing program, such as those freely available Print-to-PDF application. Then all you need is to decode PDF using some 3rd party library (which has free and opensource variance too) instead of cracking your head to reinvent the wheel. There are other Print-to-XXX application which you could also consider depending on the format you see fit.
为什么不考虑使用替代方法。就像使用中间现成的打印程序,例如那些免费提供的 Print-to-PDF 应用程序。然后,您所需要的只是使用某些 3rd 方库(也有免费和开源的差异)来解码 PDF,而不是绞尽脑汁重新发明轮子。根据您认为合适的格式,您还可以考虑其他 Print-to-XXX 应用程序。
回答by Cyril Gupta
I don't thing interpreting PDF approach is any good either. I don't know why you want to do what you want to do. Maybe you're trying to write a PDF printer of your own.
我也不认为解释 PDF 方法有什么好处。我不知道你为什么想做你想做的事。也许您正在尝试编写自己的 PDF 打印机。
I think you should really give the Windows Driver Development thing a look. If you want to develop a printer driver, I think you'll have to do it in C++.
我认为你真的应该看看 Windows 驱动程序开发。如果你想开发一个打印机驱动程序,我认为你必须用C++来做。
Good Luck.
祝你好运。
回答by PDK
If I remember correctly Microsoft does not support .NET within printer driver development. I have yet to come across a pure .NET printer driver. You will be a very brave man to do so! The website "Printer Driver Resource Toolkit" does not say that the driver has been written in .NET
如果我没记错的话,Microsoft 在打印机驱动程序开发中不支持 .NET。我还没有遇到过纯 .NET 打印机驱动程序。你将是一个非常勇敢的人这样做!网站“打印机驱动程序资源工具包”并没有说驱动程序是用 .NET 编写的

