whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

30 Visitors Online


 
...save and load global printer settings?
Autor: Rob Schoenaker
[ Print tip ]  

Tip Rating (19):  
     


{ 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.

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners