views:

255

answers:

6

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 minute 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...

+1  A: 

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.

ctacke
+3  A: 

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

Max Kielland
@Max: See question edit.
BlueRaja - Danny Pflughoeft
A: 

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!

icemanind
+1  A: 

Did you try using the Windows CE port monitor functions? http://msdn.microsoft.com/en-us/library/ms893529.aspx

Danra
That documentation is, uh, incredibly lacking. Do you have any example code? (including *where* these functions are defined - the newest documentation says `Prnport.h`, but that file doesn't exist)
BlueRaja - Danny Pflughoeft
Sorry, I only see like you that the newest documentation at http://msdn.microsoft.com/en-US/library/ee486971.aspx also refers to Prnport.h.
Danra
+1  A: 

One possibility is to enumerate devices and check if your device is being enumerated. Not sure if it works on WinCE

Chubsdad
A: 

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.

RD
Where is `IOCTL_PARALLEL_STATUS` defined? I can't seem to find it (it might not exist in Windows CE...)
BlueRaja - Danny Pflughoeft
Were you able to find the other IOCTL_PARALLEL defines? Its been a long time since I have worked with Windows CE. This msdn article (http://msdn.microsoft.com/en-us/library/aa463071.aspx) seems to imply that if the CreateFile function fails then the driver can't communicate with the printer, so you may not need to try a DeviceIOControl call.
RD
Nice, very simple and does the trick - congratulations on your bounty!
BlueRaja - Danny Pflughoeft