Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Get printer statues

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
hikehilter
Asked:
hikehilter
  • 3
1 Solution
 
mocartsCommented:
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
 
hikehilterAuthor Commented:
i need get printer other state ,
example:Error ,printing,....
0
 
mocartsCommented:
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
 
mocartsCommented:
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
 
CleanupPingCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now