简体   繁体   English

在不使用互操作/非托管代码的情况下监视打印后台处理程序

[英]Monitoring Print Spool Without Using Interop/Unmanaged Code

Background: 背景:

I'm writing an application in C# using .NET 4.0. 我正在使用.NET 4.0在C#中编写应用程序。 It prints a bunch of documents in a certain order. 它以一定顺序打印一堆文件。 The documents are of all different types and are actually printed using ShellExecute with the "print" verb. 这些文档是所有不同类型的文档,实际上是使用ShellExecute和“ print”动词打印的。

To make sure the order doesn't get jumbled, I'd like to examine the print queue for the printer involved. 为了确保订单不会混乱,我想检查所涉及打印机的打印队列。 My main loop would look like: 我的主循环如下所示:

  1. Invoke "print" action on the document 在文档上调用“打印”操作
  2. Wait for document to show up in print queue 等待文档显示在打印队列中
  3. Repeat until done 重复直到完成

How Can I Monitor The Print Queue Using Managed Code? 如何使用托管代码监视打印队列?

I found some great examples of doing similar things using unmanaged calls (Like: http://blogs.msdn.com/b/martijnh/archive/2009/08/05/printmonitor-ac-print-spooler-monitor.aspx ). 我发现了一些使用非托管调用执行类似操作的好例子(例如: http : //blogs.msdn.com/b/martijnh/archive/2009/08/05/printmonitor-ac-print-spooler-monitor.aspx )。 Also, I know how to look at the spooled files under c:\\windows\\system32\\spool... and figure things out that way. 另外,我知道如何查看c:\\ windows \\ system32 \\ spool ...下的假脱机文件,并以此方式进行查找。

Howver, none of those solutions are very satisfying ... with amount of unmanaged cod I'm calling I feel like I should just be writing the app in C++. 但是,这些解决方案都不是令人满意的……我称呼的是大量非托管鳕鱼,我觉得我应该只用C ++编写应用程序。 (And not have the .NET dependency/overhead.) (并且没有.NET依赖项/开销。)

Main Question: Is there really no way to monitor a print queue using only managed calls? 主要问题:真的没有办法仅使用托管呼叫来监视打印队列吗?

More general question: I come from the java world, and typically only use .NET languages when I want to do something OS specific or something that needs to interact with other things in the MS world. 更笼统的问题:我来自Java世界,通常只在想执行特定于OS或需要与MS世界中其他事物进行交互的内容时才使用.NET语言。 (For example SSIS components.) (例如,SSIS组件。)

It seems like every time I start a project I end up in this same mess: all kinds of calls to native functions, COM stuff, etc, etc. 似乎每次启动项目时,我都会陷入同样的​​混乱:对本机函数,COM内容等的各种调用。

Secondary Question: Is there something I'm missing about the .NET philosophy or implementation? 次要问题:关于.NET哲学或实现,我缺少什么吗? (Am I just not looking hard enough for managed libraries to do things? Is .NET the wrong choice for anything that needs to do Windows-Specific things like manipulate the print queue?) I get (or think I get) that .NET is theoretically supposed to be OS-independent.. but surely most modern operating system have printers and print queues and things like that. (我只是对托管库做事没有足够的努力吗?.NET是否是需要执行Windows特定的事情(例如操作打印队列)的错误选择?)我知道(或认为我知道).NET是从理论上讲应该独立于操作系统。.但是可以肯定的是,大多数现代操作系统都具有打印机和打印队列以及类似的东西。 (So if you had generic calls for doing these kinds of things, they could be implemented on each platform's version of the framework..) (因此,如果您有进行此类事情的通用调用,则可以在每个平台的框架版本上实现它们。)

Main Question: Take a look at the PrintQueue and LocalPrintServer class in the System.Printing namespace. 主要问题:看一下System.Printing命名空间中的PrintQueueLocalPrintServer类。

Secondary Question: .NET was not written to be OS-independent (sans Mono), it was written to be Windows version independent. 次要问题:.NET并非与操作系统无关(没有Mono),而是与Windows版本无关。 While it would be nice only deal with managed objects and managed calls, I see this as a somewhat unrealistic expectation. 尽管只处理托管对象和托管调用会很不错,但我认为这是一种不切实际的期望。 The sheer size and volume of existing C and COM functions exposed by Windows makes wrapping everything a daunting task. Windows公开的现有C和COM函数的庞大大小和体积使得打包所有内容成为一项艰巨的任务。 While i'm sure Microsoft has tons of developers on the payroll, I would say that the return on investment is quite low for such an undertaking, considering the relatively easy to use COM & P/Invoke support available. 尽管我肯定微软会有大量开发人员在薪资上,但我要说的是,考虑到相对容易使用的COM&P / Invoke支持,这种投资的投资回报率很低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM