Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 May : spyprinter - FindNextPrinterChangeNotification

www.cryer.info
Managed Newsgroup Archive

spyprinter - FindNextPrinterChangeNotification

Subject:spyprinter - FindNextPrinterChangeNotification
Posted by:"sandy" (san..@cpssoft.com)
Date:29 May 2005 20:05:38

Hi All,
First sorry for my bad english, coz it isn't my native.
Right now i'm developing a POS software, and for that purpose, i need to know the printer status, like offline or paper out and so on.

I already download spyprinter (Thanks to you Dr. Peter, it's a great stuff) and i had modified it a little.

This is some parts of the code:

procedure TPrintMonitor.Execute;
var
  vHandles : packed Record
            FindHandle,
            WakeHandle: THandle;
           end;
  vNotifyOptions : PRINTER_NOTIFY_OPTIONS;
  vNotifyType    : PRINTER_NOTIFY_OPTIONS_TYPE;
  vRetval : DWORD;
Begin
  FRunning := True;
  if Terminated then Exit;

  vNotifyOptions.Version := 2;
  vNotifyOptions.Count   := 1;
  vNotifyOptions.Flags   := PRINTER_NOTIFY_OPTIONS_REFRESH;
  vNotifyOptions.pTypes  := @vNotifyType;

  FillChar( vNotifyType, sizeof( vNotifyType ), 0 );
  vNotifyType.wType      := PRINTER_NOTIFY_TYPE;
  vNotifyType.pFields    := @CArr_PrinterField;
  vNotifyType.Count      := High( CArr_PrinterField )- Low(
                            CArr_PrinterField ) + 1;

  vHandles.WakeHandle := FWakeupEvent.Handle;
  vHandles.FindHandle :=
    FindFirstPrinterChangeNotification(
      FPrinterHandle,
      PRINTER_CHANGE_PRINTER,
      //i use this flag to check printer status
      0, @vNotifyOptions );

  if vHandles.FindHandle <> INVALID_HANDLE_VALUE then begin
    while not Terminated do begin
      vRetval := WaitForMultipleObjects( 2, @vHandles, false,
                 INFINITE );
      if not Terminated then
        if vRetval = WAIT_OBJECT_0 then
          try
            HandleNotification( vHandles.FindHandle )
          except
          end
        else
          Break;
    end;
    FindClosePrinterChangeNotification( vHandles.FindHandle );
  end
  else begin
    {$IFDEF DEBUG}
    S:= 'FindFirstPrinterChangeNotification failed for printer '+
        FJobInformation.FPrinterName+', the system error is'#13#10+
        SysErrorMessage( GetLastError );
    Windows.MessageBox( 0, Pchar(S), 'Error', MB_OK or MB_ICONSTOP );
    {$ENDIF}
  end; { Else }
end;

procedure TPrintMonitor.HandleNotification(findhandle: THandle);
var
  pni : PPrinterNotifyInfo;
  ChangeReason: Cardinal;

  function ReasonWas( flag: Cardinal ): Boolean;
  begin
    Result := (flag and ChangeReason) <> 0;
  end;
begin
  if FindNextPrinterChangeNotification(
       findhandle, ChangeReason, nil, Pointer(pni) )
  then begin
    try
      if ReasonWas( PRINTER_CHANGE_SET_PRINTER )
      then
        FPrinterInformation.FStatus := FindPrinterStatus( pni )
      else
        FPrinterInformation.FStatus := psNone;

      if FPrinterInformation.FStatus <> psNone then begin
        GetPrinterInformation( pni );
        DoPrinterChange;
      end;
    finally
      FreePrinterNotifyInfo( pni );
    end;
  end;
end;

The problem is :
  FindNextPrinterChangeNotification(
       findhandle, ChangeReason, nil, Pointer(pni) )

pni always null, i don't know what's wrong with my code??

Thanks :)

Replies:

www.cryer.info
Managed Newsgroup Archive