Most reliable way to detect printer status using c# - c#

I am writing an application in which users needs to make sure that the printer is online/working before they print the invoice.
Most of solutions on the internet point to WMI class to query for printers and their properties including PrinterStatus, PrinterState.. etc.
However that method suffers from the problem of returning Idle status all the time (check the thread here).
So is a way to use WMI to detect if printer is working or not? Or should I use another approach if any exists?
Note:
An alternative way might be using PrintQueue class, but I am not sure if I can detect the exact print jobs sent by PrintDocument class whcih I am using to print.

Related

Identify laptop screen

For my project I need a way to get data regarding screens that are connected.
In specific, I need to identify whether a monitor is a laptop internal screen or an external screen, and get all the screen data.
I need to get this information both in c++ and C#.
I read about Win32_DesktopMonitor, about EnumDisplayDevices and about Screen Class.
I read also some related questions here:
Monitor ID and Serial Number
Find Number and resolution to all monitors
EnumDisplayDevices vs WMI Win32_DesktopMonitor, how to detect active monitors?
I havn't found an answer yet. Any Ideas?
To get the information whether monitor is internal you can use WMI class WmiMonitorConnectionParams from root\wmi namespace.
Code would need to create a CimSession by connecting to WMI either through DCOM or WinRM (with authentication as needed if enumerating remote computer monitors), then call EnumerateInstances(#"root\wmi", "WmiMonitorConnectionParams") on that session.
Resulting collection will contain InstanceName (string) and VideoOutputTechnology (UInt32).
You will need both for each monitor so you can match them with the other stuff you need.
If VideoOutputTechnology is 0xFFFFFFFF, then that's the Default Monitor entry and it can be ignored. If it is 0x80000000, then it is internal. Other types of connection are documented in d3dkmdt.h header file (online documentation at the moment of this writing does not provide correct values for the enum).
The most reliable way to get model, serial number, as well as year and week of manufacture is by reading and parsing the raw EDID block (by calling WmiGetMonitorRawEEdidV1Block).
I hope you can get by on your own from here.
What you can do is query the Windows WMI classes:
http://msdn.microsoft.com/en-us/library/aa394554(v=vs.85).aspx
Those classes allows the user to collect various information about the computer (hardware, os, ...)
I don't know if you'll find the properties you need, but it might be worth a look.
You're looking for this class:
http://msdn.microsoft.com/en-us/library/aa394122(v=vs.85).aspx

Print queue monitoring: How to get info of caller application that initiated a print job?

Im currently writing a kinda print monitor app to run on a print server. The request is as follows:
When a user is printing on a specific application, the information about the application which initiated the print job is necessary. When monitoring the printer queue it gives me user information and lots of other stuff, but querying the information about the application which had initiated the printjob cannot be found.
Questions are:
Is there a C#/.NET way to find out which application (e.g. winword, excel, etc) has initiated a print job?
Is this information stored in the PrintQueue or the printspooler or anywhere else? How can this be done using the System.Printing Namespace or Win32Api ?
Any Ideas?
I am not sure but I think you could get the required info from PrintSystemJobInfo.
PrintJobInfoCollection collection = LocalPrintServer.GetDefaultPrintQueue().GetPrintJobInfoCollection();
Iterate the collection and get the PrintSystemJobInfo instances.

Using C# to detect a printer's power status

I did a lot of search and it comes with nothing.Such as below:
c# check printer status
how do i check if a printer is installed and ready using C#?
I tried all the code I found in here but none of them can tell the status of a printer's power is on or off.
The printer is always plugged in and sometimes it's powered off, so I need to make sure it's powered on and then do the print job.
Real need some help here!
question update
the windows still send the print job to print spool when my printer is off, I don't need this, because my client sometimes will deliberately turn off the printer to avoid print something that he don't need. but windows still send it to spool, when my client turn on the printer again, it prints the thing that exactly he want to avoid.
The legacy code uses kernel32.dll's CreateFile function to open LPT1 as a file and directly write string to it to accomplish then printing function. It got no way to know the status of the printer, it just write string to the parallel port. so when the printer is off, the write function got no return, hence the following code after that will never get executed.
hopefully u can understand what I am trying to do here. I want to avoid the situation to print when the printer is off.

How to find which computer send the print job?

I'm creating a postscript printer for windows 7 which will accept print jobs and forward them to real printers. It will be shared in the LAN and can receive print jobs from LAN computers. I want to find out from which computer a print job came from before forwarding them to a printer. How can I do this? Is the details available in the print job itself?
Thanks
Take a look at the GetJob and EnumJobs spooler api functions. They both return one of two structures, JOB_INFO_1 or JOB_INFO_2. Each structure contains a pMachineName field, which is the name of the machine that created the print job. You may find the following links from pinvoke.net useful.
http://www.pinvoke.net/default.aspx/Structures.JOB_INFO_1
http://www.pinvoke.net/default.aspx/winspool/EnumJobs.html
Alternatively, you may also want to look into using WMI and the Win32_PrintJob class. Specifically the HostPrintQueue property.

Transferring Print Jobs Between Printers

We are assigned to develop an application using C# that can transfer print jobs between printers. I have looked on the Windows API, and System.Printing namespace but I can't find a class or function that can do this. Is there a free library out there that is suitable for this? How will you do this using .Net or the Win32 API?
If nothing exists in the Win32 API, then there will be nothing in .NET.
Unless the printer drivers are identical then you may have problems because the printer jobs go through some processing by the driver before entering the queue.
Have you considered a single queue with multiple printers associated with it (this giving more capacity and redundancy in case of printer failure)?
Isn't this a problem of load-balancing/routing a particular job to the least busy printer?
I'm pretty sure you can just CopyFile a SPL file to a new printer port if it's the same driver. If you print in EMF, you may be able to go across different drivers.
The port names might be a bit tricky depending on how the printer(s) are attached. WinObj and the rules of dev naming will be handy for tracking it down.

Categories