Print Coil Monitoring Without Interop / Unmanaged Code

Background:

I am writing an application in C # using .NET 4.0. He prints a bunch of documents in a specific order. Documents have different types and are actually printed using ShellExecute with the verb "print".

To make sure that the order was not confused, I would like to check the print queue for the printer used. My main loop will look like this:

  • Call the print action in the document
  • Wait for the document to appear in the print queue
  • Repeat to the end

How to track a print queue using managed code?

I found some great examples of such actions using unmanaged calls (for example: http://blogs.msdn.com/b/martijnh/archive/2009/08/05/printmonitor-ac-print-spooler-monitor.aspx ). In addition, I know how to look at buffered files in the c: \ windows \ system32 \ spool ... directory and find out what happens.

Be that as it may, none of these solutions is very satisfactory ... with the amount of unmanaged cod that I call, I feel that I just have to write an application in C ++. (And do not have dependencies / .NET overhead.)

Main question: Is there no way to track the print queue using only managed calls?

A more general question: I come from the java world and usually use only .NET languages ​​when I want to do something specific OS or something that is needed to interact with other things in the MS world. (For example, SSIS components.)

It seems that every time I start a project, I find myself in the same mess: all kinds of calls for my own functions, COM materials, etc. etc.

Secondary Question: Is there something that I am missing in the philosophy or implementation of .NET? (I just don't look complicated enough for managed libraries to do something? Is .NET the wrong choice for something that needs to be done for Windows? Specific things like managing print queues?) I get (or think what I get) that it is theoretically assumed that .NET is OS independent. But, of course, most modern operating systems have printers and print queues and the like. (So, if you have general calls for such actions, they can be implemented on each platform version of the framework ..)

+6
source share
1 answer

The main question: look at PrintQueue and LocalPrintServer in the System.Printing namespace.

Secondary question: .NET was not written as OS independent (sans Mono), it was written as Windows independent. Although it would be nice to deal only with managed objects and managed calls, I see this as a somewhat unrealistic expectation. The extraordinary size and scope of the existing C and COM functions open by Windows makes this all a daunting task. Although I'm sure that Microsoft has tons of developers in the payroll, I would say that the return on investment is too small for such an undertaking, given the relatively easy to use support for COM and P / Invoke.

+5
source

Source: https://habr.com/ru/post/906960/


All Articles