Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Howto detect if a printer is offline or turned off or is not available



Permlink Replies: 9 - Last Post: Feb 20, 2018 1:49 PM Last Post By: Fred Schetterer
Michael Eriksen

Posts: 27
Registered: 6/12/11
Howto detect if a printer is offline or turned off or is not available
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 15, 2018 12:31 PM
Hello all,

I have a printer that is turned off or is offline - is it possible via a Delphi boolean routine to check for that?
A routine that returns True if a given printername is Online and False if a given printername is Offline(turned off).

I'm using Delphi XE8

Thanks in advance,
Regards Michael Eriksen

Edited by: Michael Eriksen on Feb 15, 2018 10:46 PM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Howto detect if a printer is offline or turned off or is not available
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 15, 2018 6:02 PM   in response to: Michael Eriksen in response to: Michael Eriksen
Michael Eriksen wrote:
I have a printer that is turned off or is offline - is it possible via a Delphi boolean routine to check for that?

You can't get that information from the VCL's TPrinter class. You have to open the desired printer by calling the Win32 API OpenPrinter() function directly (which TPrinter does use internally, but doesn't expose access to the opened HANDLE), and then call the Win32 API GetPrinter() function with its Level parameter set to 2 or 6 to retrieve status information.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162751.aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/dd144911.aspx

--
Remy Lebeau (TeamB)
Michael Eriksen

Posts: 27
Registered: 6/12/11
Re: Howto detect if a printer is offline or turned off or is not available
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 16, 2018 2:19 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hello,

Thanks for your reply.
But do you have an example with OpenPrinter() and GetPrinter() ??
I really appreciate that.

I have tried with the following code - but dwStatus always returns 0 whether the printer is online or offline :-(

procedure TForm1.Button1Click(Sender: TObject);
var
vHandle: THandle;
vPrinter_Info_6: PPrinterInfo6;
vSize: DWord;
bytesNeeded: DWORD;
begin
if OpenPrinter('HP Photosmart 3200 series', vHandle, nil) then begin
GetPrinter(vHandle, 6, nil, 0, @bytesNeeded );
vPrinter_Info_6 := AllocMem(bytesNeeded);
try
if GetPrinter(vHandle, 6, vPrinter_Info_6, bytesNeeded, @bytesNeeded ) then begin
With vPrinter_Info_6^ Do Begin
showmessage(inttostr(dwStatus));
end;
ClosePrinter(vHandle);
end;
finally
FreeMem(vPrinter_Info_6);
end;
end;
end;


Thanks in advance,
Michael Eriksen

You can't get that information from the VCL's TPrinter class. You have to open the desired printer by calling the Win32 API OpenPrinter() function directly (which TPrinter does use internally, but doesn't expose access to the opened HANDLE), and then call the Win32 API GetPrinter() function with its Level parameter set to 2 or 6 to retrieve status information.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd162751.aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/dd144911.aspx

--
Remy Lebeau (TeamB)

Edited by: Michael Eriksen on Feb 16, 2018 8:16 PM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Howto detect if a printer is offline or turned off or is not available [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 16, 2018 12:12 PM   in response to: Michael Eriksen in response to: Michael Eriksen
Michael Eriksen wrote:

I have tried with the following code - but dwStatus always returns
0 whether the printer is online or offline :-(

Do you have the same problem with Level=2 ?

procedure TForm1.Button1Click(Sender: TObject);
var
  vHandle: THandle;
  vPrinterInfo: PPrinterInfo2;
  vSize: DWord;
  bytesNeeded: DWORD;
begin
  if OpenPrinter('HP Photosmart 3200 series', vHandle, nil) then
  try
    GetPrinter(vHandle, 2, nil, 0, @bytesNeeded);
    vPrinterInfo := AllocMem(bytesNeeded);
    try
      if GetPrinter(vHandle, 2, vPrinterInfo, bytesNeeded,
@bytesNeeded) then
      begin
        ShowMessage(IntToStr(vPrinterInfo.Status));
      end;
    finally
      FreeMem(vPrinterInfo);
    end;
  finally
    ClosePrinter(vHandle);
  end;
end;


--
Remy Lebeau (TeamB)
Michael Eriksen

Posts: 27
Registered: 6/12/11
Re: Howto detect if a printer is offline or turned off or is not available [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 16, 2018 1:07 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
I have tried with level 2 as well - and it gives me the same result :-(
Status=0 whether the printer is online or offline :-(

Remy Lebeau (TeamB) wrote:
Michael Eriksen wrote:

I have tried with the following code - but dwStatus always returns
0 whether the printer is online or offline :-(

Do you have the same problem with Level=2 ?

procedure TForm1.Button1Click(Sender: TObject);
var
  vHandle: THandle;
  vPrinterInfo: PPrinterInfo2;
  vSize: DWord;
  bytesNeeded: DWORD;
begin
  if OpenPrinter('HP Photosmart 3200 series', vHandle, nil) then
  try
    GetPrinter(vHandle, 2, nil, 0, @bytesNeeded);
    vPrinterInfo := AllocMem(bytesNeeded);
    try
      if GetPrinter(vHandle, 2, vPrinterInfo, bytesNeeded,
@bytesNeeded) then
      begin
        ShowMessage(IntToStr(vPrinterInfo.Status));
      end;
    finally
      FreeMem(vPrinterInfo);
    end;
  finally
    ClosePrinter(vHandle);
  end;
end;


--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Howto detect if a printer is offline or turned off or is not available [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 16, 2018 1:37 PM   in response to: Michael Eriksen in response to: Michael Eriksen
Michael Eriksen wrote:

I have tried with level 2 as well - and it gives me the same result
:-( Status=0 whether the printer is online or offline :-(

Well, then maybe your printer driver is buggy, or isn't reporting
status updates to Windows. Does the correct status appear in any of
the printer's own configuration/status UIs, or in the Windows control
panel?

--
Remy Lebeau (TeamB)
Michael Eriksen

Posts: 27
Registered: 6/12/11
Re: Howto detect if a printer is offline or turned off or is not available [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 16, 2018 2:36 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hello again,

Thanks for your reply.

I can't find any configuration/status UIs or something like that :-(
But the HP printerdriver detects when I open the door to see the cartridges - so something may allready be detected by the driver!

Do you have any workaround or any further ideas?

Regards
Michael Eriksen

Remy Lebeau (TeamB) wrote:
Michael Eriksen wrote:

I have tried with level 2 as well - and it gives me the same result
:-( Status=0 whether the printer is online or offline :-(

Well, then maybe your printer driver is buggy, or isn't reporting
status updates to Windows. Does the correct status appear in any of
the printer's own configuration/status UIs, or in the Windows control
panel?

--
Remy Lebeau (TeamB)
Fred Schetterer

Posts: 83
Registered: 5/20/99
Re: Howto detect if a printer is offline or turned off or is not available [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 20, 2018 12:11 PM   in response to: Michael Eriksen in response to: Michael Eriksen
Michael Eriksen wrote:
I have tried with level 2 as well - and it gives me the same result
:-( Status=0 whether the printer is online or offline :-(

Same here; using Windows 10 and I've tried it on a Network Samsung Printer as well as the built in FAX, XPS and PDF Writers.
All calls succeed but Status is Zero no matter what.

EDIT: [How to get the status of a printer and a print job|https://support.microsoft.com/en-us/help/160129/how-to-get-the-status-of-a-printer-and-a-print-job]

Consequently, a system Printer reports no status when the Printer queue is empty. In this state, the Printer is assumed ready to accept print jobs. This is a valid assumption even if the physical printer is in an error state such as off-line. The operating system considers the Printer ready to accept print jobs even if, for some reason, it cannot complete delivery to the physical printer. Such a circumstance is considered an error state in the operating system that must be addressed by the user. It is not considered an error reportable to the application that is allowed to complete the spooling of the print job successfully.

Edited by: Fred Schetterer on Feb 20, 2018 1:47 PM

Tim Frost

Posts: 21
Registered: 11/21/99
Re: Howto detect if a printer is offline or turned off or is not available [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 17, 2018 5:55 AM   in response to: Michael Eriksen in response to: Michael Eriksen
You should be able to find from WMI whatever is available. I suggest
downloading the free WMI Explorer, double-clicking ROOT\CIMV2, then
Win32_Printer, then your printer. Look at the PrinterStatus and
DetectedErrorState properties, and perhaps others, and see what values
you get after switching your printer on/off or online/online, or
opening a door (and clicking Refresh Object each time). The values may
be different for different printers or connection types (eg
USB/Network), and may need further research to interpret accurately.

Then find a good WMI library component and just call it in your program
to get the values when you need them. I have used those from Magenta
Systems and Mitec, but there are others also. Of course going via WMI
may be overkill for what you need, but in the first instance WMI
Explorer should enable you to discover every printer parameter (among
thousands of others) that is easily available on your system.
Michael Eriksen

Posts: 27
Registered: 6/12/11
Re: Howto detect if a printer is offline or turned off or is not available [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 20, 2018 8:10 AM   in response to: Tim Frost in response to: Tim Frost
Hello,
Thanks for taking your time to write and help :-)
I have tested your suggestion with WMI Explorer - but I got an unknown status for my printer. So my conclusion is that this function is not available on my printer/printerdriver (and maybe other printers too). And because of that I have determined not to implement this function.

So thanks for any advice to help me.

Regards
Michael Eriksen

Tim Frost wrote:
You should be able to find from WMI whatever is available. I suggest
downloading the free WMI Explorer, double-clicking ROOT\CIMV2, then
Win32_Printer, then your printer. Look at the PrinterStatus and
DetectedErrorState properties, and perhaps others, and see what values
you get after switching your printer on/off or online/online, or
opening a door (and clicking Refresh Object each time). The values may
be different for different printers or connection types (eg
USB/Network), and may need further research to interpret accurately.

Then find a good WMI library component and just call it in your program
to get the values when you need them. I have used those from Magenta
Systems and Mitec, but there are others also. Of course going via WMI
may be overkill for what you need, but in the first instance WMI
Explorer should enable you to discover every printer parameter (among
thousands of others) that is easily available on your system.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02