Is there a way in Windows (which works in Windows CE) to check if a printer is attached and communicating to LPT1 in C++?
[Edit] More info:
We are currently working with a generic Windows CE printer driver - pcl.dll
- by passing it into CreateDC
, to get the DC for the printer.
We can't call PrintDlg()
to show the print dialog because it is "too complicated looking," but we also can't call it with PD_RETURNDEFAULT
because we do not want to use the default printer. So, we are manually setting up a DEVMODE
structure to pass in to CreateDC
.
After we have the DC, we call GetDeviceCaps()
to get the info for the printer (such as page-width, etc), then StartPage()
/EndPage()
/ExtTextOut()
to print.
However, if there is no printer attached, the program freezes for about a m开发者_如何学编程inute before giving an "Abort/retry/fail?" dialog (I don't know what point in this process it is freezing). Other software doesn't freeze when you attempt to print, so there must be a way of preventing this...
When you work with printers in Windows you actually never supposed to work with the port directly, but through the printer driver interface. This obsoletes the knowledge of how to communicate and gives you a toolbox that is the same for all printers, regardless of brand and port.
The Windows API gives you many possibilities to extract extra information from the driver. During my 14 years of printer development, Microsoft has never added support for rich printer status (although I don't know if they have changed that in Windows 7). We, as many other printer developers had to extend the printer driver to present more information to the application.
You should ask your printer developer if they have a Windows CE driver. I'm not sure if the DEVICE_CHANGE message is generated when plugging in /out a parallel printer. It does for USB printers (No need to mess around in the registry).
You can read more about the Printing subsystem here
I would also recommend enumerating devices, but you could try the following functions to see if it hangs quickly and gracefully (I don't currently have any way of testing this...):
CreateFile("LPT1:", 0, 0, NULL, OPEN_EXISTING, ...);
DeviceIOControl(HANDLE, IOCTL_PARALLEL_STATUS, ...);
It is possible that this returns a failure better than trying to print with the DC. If it works, don't forget to call CloseHandle() on the HANDLE returned from CreateFile before opening your DC for printing.
If it's a USB printer you could look at the USB printer class driver registry entry to see if it's plugged in before proceeding.
Did you try using the Windows CE port monitor functions? http://msdn.microsoft.com/en-us/library/ms893529.aspx
One possibility is to enumerate devices and check if your device is being enumerated. Not sure if it works on WinCE
I don't know if you are using .NET managed code or not, but here is a link on how to use LPT ports using VB.NET (which can be converted to C++ .NET managed): http://support.microsoft.com/default.aspx?scid=kb;en-us;823179
Hope this helps!
精彩评论