...save and load global printer settings?

Author: Rob Schoenaker

Category: Printing

{ Changing the properties of a global printer object is a tedious task.
The code below allows to save current settings of any installed printer
to a stream using the SavePrinterInfo procedure. These settings include
vendor specific settings. }

{ Using LoadPrinterInfo, one can retrieve saved settings and apply these
to the printer. }

unit PrinterIO;

interface

uses
  
Classes;

procedure SavePrinterInfo(APrinterName: PChar; ADestStream: TStream);
procedure LoadPrinterInfo(APrinterName: PChar; ASourceStream: TStream);

implementation

uses
  
Windows, SysUtils, WinSpool;

procedure SavePrinterInfo(APrinterName: PChar; ADestStream: TStream);
var
  
HPrinter : THandle;
  InfoSize,
  BytesNeeded: Cardinal;
  PI2: PPrinterInfo2;
  PrinterDefaults: TPrinterDefaults;
begin
  with 
PrinterDefaults do
  begin
    
DesiredAccess := PRINTER_ACCESS_USE;
    pDatatype := nil;
    pDevMode := nil;
  end;
  if OpenPrinter(APrinterName, HPrinter, @PrinterDefaults) then
  try
    
SetLastError(0);
    //Determine the number of bytes to allocate for the PRINTER_INFO_2 construct...
    
if not GetPrinter(HPrinter, 2, nil, 0, @BytesNeeded) then
    begin
      
//Allocate memory space for the PRINTER_INFO_2 pointer (PrinterInfo2)...
      
PI2 := AllocMem(BytesNeeded);
      try
        
InfoSize := SizeOf(TPrinterInfo2);
        if GetPrinter(HPrinter, 2, PI2, BytesNeeded, @BytesNeeded) then
          
ADestStream.Write(PChar(PI2)[InfoSize], BytesNeeded - InfoSize);
      finally
        
FreeMem(PI2, BytesNeeded);
      end;
    end;
  finally
    
ClosePrinter(HPrinter);
  end;
end;

procedure LoadPrinterInfo(APrinterName: PChar; ASourceStream: TStream);
var
  
HPrinter : THandle;
  InfoSize,
  BytesNeeded: Cardinal;
  PI2: PPrinterInfo2;
  PrinterDefaults: TPrinterDefaults;
begin
  with 
PrinterDefaults do
  begin
    
DesiredAccess := PRINTER_ACCESS_USE;
    pDatatype := nil;
    pDevMode := nil;
  end;
  if OpenPrinter(APrinterName, HPrinter, @PrinterDefaults) then
  try
    
SetLastError(0);
    //Determine the number of bytes to allocate for the PRINTER_INFO_2 construct...
    
if not GetPrinter(HPrinter, 2, nil, 0, @BytesNeeded) then
    begin
      
//Allocate memory space for the PRINTER_INFO_2 pointer (PrinterInfo2)...
      
PI2 := AllocMem(BytesNeeded);
      try
        
InfoSize := SizeOf(TPrinterInfo2);
        if GetPrinter(HPrinter, 2, PI2, BytesNeeded, @BytesNeeded) then
        begin
          
PI2.pSecurityDescriptor := nil;
          ASourceStream.ReadBuffer(PChar(PI2)[InfoSize], BytesNeeded - InfoSize);
          // Apply settings to the printer
          
if DocumentProperties(0, hPrinter, APrinterName, PI2.pDevMode^,
                                PI2.pDevMode^, DM_IN_BUFFER or DM_OUT_BUFFER) = IDOK then
          begin
            
SetPrinter(HPrinter, 2, PI2, 0);  // Ignore the result of this call...
          
end;
        end;
      finally
        
FreeMem(PI2, BytesNeeded);
      end;
    end;
  finally
    
ClosePrinter(HPrinter);
  end;
end;

end.

 

printed from
www.swissdelphicenter.ch
developers knowledge base