Board index » delphi » How to Get the Handle to the Printer - Not Printer.Handle

How to Get the Handle to the Printer - Not Printer.Handle

I want to add jobs to the Spooler and need a handle to the printer - how can I
get this.

Note that Printer.Handle is the handle (hDC) to the device context, not to the
printer.

When I try to use Printer.Handle in the AddJob() API call it returns an
'Invalid Handle' error code.

Thanks

Alan Lloyd
alangll...@aol.com

 

Re:How to Get the Handle to the Printer - Not Printer.Handle


In article <1998050420501600.QAA10...@ladder01.news.aol.com>,

Quote
alangll...@aol.com (AlanGLLoyd) writes:
>I want to add jobs to the Spooler and need a handle to the printer - how can
>I
>get this.

I've solved this myself <g>

You have to open the printer using the printer name and it returns the handle -
but its not the full name in the Printer.Printers[Printer.PrinterIndex] string
- its that name stripped of the port (ie the " on LPT1:") part.

As in :-

var
  PtrName : PChar;
  hPrinter : THandle;
const
  PortSuffix : PChar = ' on ';
begin
  {get printer handle}
  PtrName := PChar(Printer.Printers[Printer.PrinterIndex]);
  StrPos(PtrName, PortSuffix)^ := #0;    {remove port suffix from name}
  if not OpenPrinter(PtrName, hPrinter, nil) then . . . etc etc

Alan Lloyd
alangll...@aol.com

Other Threads