Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the printer model in Delphi?

Tags:

c#

delphi

When I plug my HP Laserjet 3015, Windows detects the correct model and then tries to install the appropriate drivers.

How can I detect the model of connected printer(s)? I don't want to use the list of installed printers because a Zebra printer can be installed with a Generic/Text only driver.

I'm a Delphi and C# programmer, so any tips in any language will be appreciated.

like image 207
vIceBerg Avatar asked Oct 16 '08 12:10

vIceBerg


2 Answers

You can send a

~HI

to the Zebra printer and it should return its model number and also fw version

like image 172
Ovi Tisler Avatar answered Oct 12 '22 15:10

Ovi Tisler


Recently I made a little demo with this. Just put a TComboBox and a TMemo on a Form and replace the code with this:

unit Unit1;

interface

uses
  Windows, StdCtrls, Classes, Controls, Forms;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Memo1: TMemo;
    procedure ComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  Printers, WinSpool, SysUtils;

type
  _DRIVER_INFO_6A = record
    cVersion: DWORD;
    pName: PAnsiChar;             
    pEnvironment: PAnsiChar;      
    pDriverPath: PAnsiChar;       
    pDataFile: PAnsiChar;         
    pConfigFile: PAnsiChar;       
    pHelpFile: PAnsiChar;         
    pDependentFiles: PAnsiChar;   
    pMonitorName: PAnsiChar;      
    pDefaultDataType: PAnsiChar;  
    pszzPreviousNames: PAnsiChar;
    ftDriverDate: TFileTime;
    dwlDriverVersion: Int64;
    pszMfgName: PAnsiChar;
    pszOEMUrl: PAnsiChar;
    pszHardwareID: PAnsiChar;
    pszProvider: PAnsiChar;
  end;
  TDriverInfo6A = _DRIVER_INFO_6A;
  PDriverInfo6A = ^TDriverInfo6A;
  PDriverInfo6 = PDriverInfo6A;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBox1.Items.Assign(Printer.Printers);
  ComboBox1.ItemIndex := 0;
  ComboBox1Change(nil);
end;

function FileTimeToDateTime(ft: TFileTime): TDateTime;
var
  st: TSystemTime;
  lt: TFileTime;
begin
  FillChar(st, SizeOf(st), 0);
  FillChar(lt, SizeOf(lt), 0);
  FileTimeToLocalFileTime(ft, lt);
  FileTimeToSystemTime(lt, st);
  result := SystemTimeToDateTime(st);
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  hPrinter: THandle;
  PrtName: String;
  DriverInfo: PDriverInfo6;
  dwNeeded: DWORD;
begin
  Memo1.Clear;
  PrtName := Combobox1.Text;
  OpenPrinter(PChar(PrtName), hPrinter, nil);
  DriverInfo := nil;
  try
    GetPrinterDriver(hPrinter, nil, 6, DriverInfo, 0, dwNeeded);
    GetMem(DriverInfo, dwNeeded);
    try
      if GetPrinterDriver(hPrinter, nil, 6, DriverInfo, dwNeeded, dwNeeded) then begin
        Memo1.Lines.Add('cVersion: ' + IntToStr(DriverInfo.cVersion));
        Memo1.Lines.Add('pName: '+StrPas(DriverInfo.pName));
        Memo1.Lines.Add('pEnvironment: '+StrPas(DriverInfo.pEnvironment));
        Memo1.Lines.Add('pDriverPath: '+StrPas(DriverInfo.pDriverPath));
        Memo1.Lines.Add('pDataFile: '+StrPas(DriverInfo.pDataFile));
        Memo1.Lines.Add('pConfigFile: '+StrPas(DriverInfo.pConfigFile));
        Memo1.Lines.Add('pHelpFile: '+StrPas(DriverInfo.pHelpFile));
        Memo1.Lines.Add('pDependentFiles: '+StrPas(DriverInfo.pDependentFiles));
        Memo1.Lines.Add('pMonitorName: '+StrPas(DriverInfo.pMonitorName));
        Memo1.Lines.Add('pDefaultDataType: '+StrPas(DriverInfo.pDefaultDataType));
        Memo1.Lines.Add('pszzPreviousNames: '+StrPas(DriverInfo.pszzPreviousNames));
        Memo1.Lines.Add('ftDriverDate: '+DateTimeToStr(FileTimeToDateTime(DriverInfo.ftDriverDate)));
        Memo1.Lines.Add('dwlDriverVersion: '+IntToStr(DriverInfo.dwlDriverVersion));
        Memo1.Lines.Add('pszMfgName: '+StrPas(DriverInfo.pszMfgName));
        Memo1.Lines.Add('pszOEMUrl: '+StrPas(DriverInfo.pszOEMUrl));
        Memo1.Lines.Add('pszHardwareID: '+StrPas(DriverInfo.pszHardwareID));
        Memo1.Lines.Add('pszProvider: '+StrPas(DriverInfo.pszProvider));
      end else
        Memo1.Lines.Add('No Info needed = ' + IntToStr(dwNeeded));
    finally
      FreeMem(DriverInfo);
    end;
  finally
    ClosePrinter(hPrinter);
  end;
end;

end.

edit: removed the unnecessary function GetDriverNameByOSPrinterName

BTW: In pName you have the Name of the Driver not the Name of the Printer. The Printername is changeable in Windows, so if you want go sure, use the Printerdrivername.

like image 30
Roman Ganz Avatar answered Oct 12 '22 16:10

Roman Ganz