Receive notifications when the printer status changes

I would like to develop an application that displays a message when the printer prints.

I created a small test application for the console, but for some reason I don’t get any notification of a change in the printer state. It should be noted that when I checked the job status in this way (with JOB_NOTIFY_TYPE and JOB_NOTIFY_FIELD_STATUS), notifications about the job status were indeed received.

Does the printer not send notifications? (And is it related to the printer type and/or driver?)

Here is the code to test:

program PrinterMonitor;

{$APPTYPE CONSOLE}

uses
  Winapi.Windows,
  Winapi.WinSpool;

const
  PrinterField: WORD = PRINTER_NOTIFY_FIELD_STATUS;

var
  PrinterName: string;
  hStopEvent: THandle;
  hPrinter: THandle;
  hPrintNotify: THandle;
  pno: TPrinterNotifyOptions;
  pnot: TPrinterNotifyOptionsType;
  pni: Pointer;
  dwChange: DWORD;
  WaitHandles: array[0..1] of THandle;

function CtrlHandler(fdwCtrlType: DWORD): BOOL; stdcall;
begin
  Result := False;
  case fdwCtrlType of
    CTRL_C_EVENT, CTRL_CLOSE_EVENT:
    begin
      SetEvent(hStopEvent);
      Result := True;
    end;
  end;
end;

procedure GetPrinterNotifyData(const Info: TPrinterNotifyInfo);
var
  I: Integer;
begin
  for I := 0 to Info.Count - 1 do
  begin
    with Info.aData[I] do
    begin
      case wType of
        PRINTER_NOTIFY_TYPE:
        begin
          case Field of
            PRINTER_NOTIFY_FIELD_STATUS: WriteLn('Printer status: ', NotifyData.adwData[0]);
          end;
        end;
      end;
    end;
  end;
end;

begin
  if ParamCount < 1 then
  begin
    WriteLn('No printer name specified.');
    Exit;
  end;
  PrinterName := ParamStr(1); //pass a printer name as argument

  OpenPrinter(PWideChar(PrinterName), hPrinter, nil);
  if hPrinter = 0 then
  begin
    WriteLn('Cannot open printer "', PrinterName, '". ErrorCode: ', GetLastError);
    Exit;
  end;

  pnot.wType := PRINTER_NOTIFY_TYPE;
  pnot.Count := 1;
  pnot.pFields := @PrinterField;

  pno.Version := 2;
  pno.Count := 1;
  pno.pTypes := @pnot;

  hStopEvent := CreateEvent(nil, True, False, nil);
  if hStopEvent = 0 then
  begin
    WriteLn('Cannot create stop event. ErrorCode: ', GetLastError);
    ClosePrinter(hPrinter);
    Exit;
  end;

  hPrintNotify := FindFirstPrinterChangeNotification(hPrinter, 0, 0, @pno);
  if hPrintNotify = 0 then
  begin
    WriteLn('Cannot start monitoring. ErrorCode: ', GetLastError);
    ClosePrinter(hPrinter);
    CloseHandle(hStopEvent);
    Exit;
  end;

  WaitHandles[0] := hStopEvent;
  WaitHandles[1] := hPrintNotify;

  SetConsoleCtrlHandler(@CtrlHandler, True); // handling CTRL+C & close console events for stop monitoring

  WriteLn('==== Monitor started ====');
  while True do
  begin
    case WaitForMultipleObjects(2, @WaitHandles, False, INFINITE) of
      WAIT_OBJECT_0: // hStopEvent signalled
      begin
        WriteLn('==== Monitor stopped ====');
        Break;
      end;
      WAIT_OBJECT_0+1: // hPrintNotify signalled
      begin
        if not FindNextPrinterChangeNotification(hPrintNotify, dwChange, nil, pni) then
        begin
          WriteLn('Cannot get notify changes data.');
          Break;
        end;
        GetPrinterNotifyData(PPrinterNotifyInfo(pni)^);
        FreePrinterNotifyInfo(PPrinterNotifyInfo(pni));
      end;
      else
      begin
        WriteLn('Wait failed');
        Break;
      end;
    end;
  end;
  FindClosePrinterChangeNotification(hPrintNotify);
  ClosePrinter(hPrinter);
  CloseHandle(hStopEvent);
end.

2

When I run your code and then pause and resume the print queue, it works fine:

==== Monitor started ====
Printer status: 131073
Printer status: 131072

The value 131073 = 0x20001, which is comprised of the enums:

PRINTER_STATUS_TONER_LOW | PRINTER_STATUS_PAUSED

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật