[DUG] Printer problem
Robert Martin
rob at chreos.co.nz
Wed Jan 13 16:53:51 NZDT 2016
Hi
Further to my original post I have identified the following additional info
1. The code works without fix if I just use the Printer. However this
code is part of a multi threaded system so I don't want to use the
'global' printer instance.
2. The fix only needs to have the 'SelPrinter.SetPrinter(Device,
Driver, Port, 0);' added. This sets the 'default' printer the
SelPrinter accesses. In the original fix 'hDevMode' returned by both
calls to GetPrinter is the same, so the only thing that is changing is
the data retrieved by GlobalLock.
Thanks
Rob
On 13/01/2016 2:29 p.m., Robert Martin wrote:
> 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
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2016.0.7294 / Virus Database: 4489/11386 - Release Date: 01/12/16
>
>
More information about the Delphi
mailing list