?
Solved

Get printer statues

Posted on 2003-02-23
7
Medium Priority
?
497 Views
Last Modified: 2007-12-19
i need get my printer state,but i use this function get printer state ,can't get printer state, only get 'PAUSE' state .


unit Frmmain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Printers,WinSpool;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
     function GetPrinter(Index:integer):Cardinal;
     function GetPrinterStatues(Index:Cardinal):string;

  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

//Return Printer State
//ParameterF
//Modify DateTimeF
//AuthF
function TForm1.GetPrinter(Index:integer):Cardinal;
var
 Device, Driver, Port: array[0..511] of Char;
 hDevMode: THandle;
 hPrinter: THandle;
 pInfo2: ^TPrinterInfo2;
 Size: DWORD;
begin
  Printer.PrinterIndex := Index;
  Printer.GetPrinter(Device, Driver, Port, hDevMode);
  Win32Check(OpenPrinter(Device, hPrinter, Nil));
  try
    WinSpool.GetPrinter(hPrinter, 2, pInfo2, 0, @size);
    GetMem(pInfo2, size);
    try
     if  Win32Check(WinSpool.GetPrinter(hPrinter, 2, pInfo2, Size, @size)) then
       begin
    //This Code is Test//
    //Test Star//
        memo1.Lines.Add('First');
        Memo1.Lines.Add(pinfo2.pServerName);
        memo1.Lines.Add(pinfo2.pPrinterName );
        memo1.Lines.Add(pinfo2.pShareName );
        memo1.Lines.Add('------------------------');
   //Test End
        Result:=pinfo2.Status;
        end;
      finally
      FreeMem(pInfo2);
    end;
  finally
    ClosePrinter(hPrinter);
  end;
end;

//Return Text
//ParamterF
//Modify DatetimeF
//AuthF

function TForm1.GetPrinterStatues(Index:Cardinal):string;
begin
//this paramter is NT and Win95.
Case index of
 0                               : Result:='OK';
 PRINTER_STATUS_PAUSED           : Result:='Printer PAUSED';
 PRINTER_STATUS_PENDING_DELETION : Result:='Printer PENDING DELETION';
 PRINTER_STATUS_BUSY             : Result:='Printer BUSY';
 PRINTER_STATUS_DOOR_OPEN        : Result:='Printer DOOR OPEN';
 PRINTER_STATUS_ERROR            : Result:='Printer ERROR';
 PRINTER_STATUS_INITIALIZING     : Result:='Printer INITIALIZING';
 PRINTER_STATUS_IO_ACTIVE        : Result:='Printer IO ACTIVE';
 PRINTER_STATUS_MANUAL_FEED      : Result:='Printer MANUAL FEED';
 PRINTER_STATUS_NO_TONER         : Result:='Printer NO TONER';
 PRINTER_STATUS_NOT_AVAILABLE    : Result:='Printer NOT AVAILABLE';
 PRINTER_STATUS_OFFLINE          : Result:='Printer OFFLINE';
 PRINTER_STATUS_OUT_OF_MEMORY    : Result:='Printer OUT OF MEMORY';
 PRINTER_STATUS_OUTPUT_BIN_FULL  : Result:='Printer OUTPUT BIN FULL';
 PRINTER_STATUS_PAGE_PUNT        : Result:='Printer PAGE PUNT';
 PRINTER_STATUS_PAPER_JAM        : Result:='Printer PAPER JAM';
 PRINTER_STATUS_PAPER_OUT        : Result:='Printer PAPER OUT';
 PRINTER_STATUS_PAPER_PROBLEM    : Result:='Printer PAPER PROBLEM';
 PRINTER_STATUS_PRINTING         : Result:='Printer PRINTING';
 PRINTER_STATUS_PROCESSING       : Result:='Printer PROCESSING';
 PRINTER_STATUS_TONER_LOW        : Result:='Printer TONER LOW';
 PRINTER_STATUS_USER_INTERVENTION :Result:='Printer USER INTERVENTION';
 PRINTER_STATUS_WAITING          : Result:='Printer WAITING';
 PRINTER_STATUS_WARMING_UP       : Result:='Printer WARMING UP';
else
  result:='Cant Get Printer status!,Return Printer Stat ID:'+Inttostr(Index);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Clear;
Memo1.Lines.Add (GetPrinterStatues(GetPrinter(StrToInt(edit1.text))));
Memo1.Lines.Add(GetPrinterStatues(GetPrinter(-1)));
end;
0
Comment
Question by:hikehilter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 8008584
don't know, but on my WinXP with three printers all works ok.
maybe check in Control Panel - Pritners - YourPrinterName  whether it's not realy paused (with menu item)
wbr, mo.
0
 

Author Comment

by:hikehilter
ID: 8008790
i need get printer other state ,
example:Error ,printing,....
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8023914
look what I found in MS MSDN site in GetPrinter function reference!
--
Security Alert   The pDevMode member in the PRINTER_INFO_2, PRINTER_INFO_8, and PRINTER_INFO_9 structures can be NULL. When this happens, the printer is unusable until the driver is reinstalled successfully.
--
maybe this is the problem?
mo.
0
 
LVL 9

Accepted Solution

by:
mocarts earned 1200 total points
ID: 8023930
and yes, forgot..
when you compare against printer state you should use:

if (Index and PRINTER_STATUS_PAUSED) = PRINTER_STATUS_PAUSED then Result := Result + 'Printer paused.' + #13#10; // adding multiple states in result

as status field can contain more than one state and if you do not mask (and) state you will never get that state.
0
 

Expert Comment

by:CleanupPing
ID: 9474637
hikehilter:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question