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

51 Visitors Online


 
...get the printer driver version?
Autor: Martin Deas
[ Print tip ]  

Tip Rating (9):  
     


{1.}
uses
  
Printers, Winspool;


procedure TForm1.FormCreate(Sender: TObject);
begin
  
ComboBox1.Items.Assign(Printer.Printers);
end;


procedure TForm1.ComboBox1Change(Sender: TObject);
var
  
hPrinter: THandle;
  PrtName: string;
  DriverInfo: PDriverInfo2;
  dwNeeded: DWORD;
begin
  
Memo1.Clear;
  PrtName := Combobox1.Text;
  OpenPrinter(PChar(PrtName), hPrinter, nil);
  GetPrinterDriver(hPrinter, nil, 2, DriverInfo, 0, dwNeeded);
  GetMem(DriverInfo, dwNeeded);
  if GetPrinterDriver(hPrinter, nil, 2, DriverInfo, dwNeeded, dwNeeded) then
  begin
    
// Add infos to Memo1
    
Memo1.Lines.Add('Version: ' + IntToStr(DriverInfo.cVersion));
    Memo1.Lines.Add(StrPas(DriverInfo.pName));
    Memo1.Lines.Add(StrPas(DriverInfo.pEnvironment));
    Memo1.Lines.Add(StrPas(DriverInfo.pDriverPath));
    Memo1.Lines.Add(StrPas(DriverInfo.pDataFile));
    Memo1.Lines.Add(StrPas(DriverInfo.pConfigFile));
  end
  else
    
Memo1.Lines.Add('No Info needed = ' + IntToStr(dwNeeded));

  ClosePrinter(hPrinter);
  FreeMem(DriverInfo);
end;

{2.}
{****************************}
{ Another Code by P. Below }

unit Unit1;

interface

uses
  
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, WinSpool;

type
  
TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    
{ Private-Deklarationen }
    
pEnumDriversData: PDriverInfo2;

  public
    
{ Public-Deklarationen }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ListBox1Click(Sender: TObject);
var
  
pTemp: PDriverInfo2;
begin
  with 
Sender as TListbox do 
  begin
    if 
ItemIndex >= 0 then 
    begin
      
memo1.Clear;
      pTemp := PDriverInfo2(Items.Objects[ItemIndex]);
      with memo1.Lines, pTemp^ do 
      begin
        
Add(Format('cVersion:'#9#9'%d', [cVersion]));
        Add(Format('pName:'#9#9'%s', [pName]));
        Add(Format('pEnvironment:'#9'%s', [pEnvironment]));
        Add(Format('pDriverPath:'#9'%s', [pDriverPath]));
        Add(Format('pDataFile:'#9#9'%s', [pDataFile]));
        Add(Format('pConfigFile:'#9'%s', [pConfigFile]));
      end;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  
i, bytesNeeded: DWORD;
  pTemp: PDriverInfo2;
  NumDrivers: DWORD;
begin
  
listbox1.Clear;
  if Assigned(pEnumDriversData) then
    
FreeMem(pEnumDriversData);
  // get number of installed drivers
  
Numdrivers := 0;
  bytesNeeded := 0;
  EnumPrinterDrivers(nilnil, 2, nil, 0, bytesNeeded, NumDrivers);
  if bytesNeeded = 0 then 
  begin
    
ShowMessage('No printer drivers installed!');
    Exit;
  end;

  // allocate memory for the driver data
  
GetMem(pEnumDriversData, bytesNeeded);
  // fetch driver data
  
if EnumPrinterDrivers(nilnil, 2, pEnumDriversData, bytesNeeded,
    bytesNeeded, NumDrivers) then 
  begin
    
// add drivers to listbox1
    
pTemp := pEnumDriversData;
    for i := 1 to Numdrivers do 
    begin
      
listbox1.Items.AddObject(pTemp^.pName, TObject(pTemp));
      Inc(pTemp);
      // Note: Inc increments a pointer by the size of its base type!
    
end;
    listbox1.ItemIndex := 0;
    listbox1Click(listbox1);
  end
  else
    
RaiseLastWin32Error;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if 
Assigned(pEnumDriversData) then
    
FreeMem(pEnumDriversData);
end;

end.

 

Rate this tip:

poor
very good


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