[DUG] Printer problem

Robert Martin rob at chreos.co.nz
Wed Jan 13 14:29:37 NZDT 2016


Hi All, happy New Year

I am having some problems reading the paper source (tray) from the 
printer.  We use the following code....



var
     SelPrinter          : TPrinter;
     Buffer1             : Array[0..255] of char;
     Buffer2             : Array[0..255] of char;
     Buffer3             : Array[0..255] of char;

     Device              : pChar;
     Driver              : pChar;
     Port                : pchar;

     hDevMode            : THandle;
     PDevMode            : pDeviceMode;
begin
     Result := -1;       //Indicates failure

     Device := Buffer1;
     Driver := Buffer2;
     Port   := Buffer3;

     SelPrinter := TPrinter.Create;
     try
         SelPrinter.PrinterIndex := 
SelPrinter.Printers.IndexOf(aPrinterName);

         SelPrinter.GetPrinter(Device, Driver, Port, hDevMode);


         if (hDevMode <> 0) then begin
             try
                 try
                     pDevMode := GlobalLock(hDevMode);

                     Result := pDevMode^.dmDefaultSource;

                     ShowMessage(pDevMode^.dmDeviceName);
                 finally
                     GlobalUnlock(hDevMode);
                 end;

                 pDevMode := nil;
             except
                 //Ignore errors
             end;
         end;
     finally
         SelPrinter.Free;
     end;



We thought this code worked but have discovered that if the printer 
being checked is not the system default printer the 
SelPrinter.GetPrinter(Device, Driver, Port, hDevMode); call returns the 
Device, Driver and Port of the requested (non default) printer but the 
hDevMode is that of the system default printer !!!!!

Doing the following makes it work correctly

         SelPrinter.PrinterIndex := 
SelPrinter.Printers.IndexOf(aPrinterName);

         SelPrinter.GetPrinter(Device, Driver, Port, hDevMode);

         //Code added
         SelPrinter.SetPrinter(Device, Driver, Port, 0);
         SelPrinter.GetPrinter(Device, Driver, Port, hDevMode);


This is pretty 'Yuk' though.  I am passing in 0 to the SetPrinter which 
doesn't seem right.  Feels like a Delphi bug, any suggestions ?

Thanks
Rob




More information about the Delphi mailing list