如何在Delphi中检测打印机型号?

时间:2008-10-16 12:59:28

标签: c# delphi

当我插入HP Laserjet 3015时,Windows会检测到正确的型号,然后尝试安装相应的驱动程序。

如何检测已连接打印机的型号?我不想使用已安装的打印机列表,因为Zebra打印机可以安装Generic / Text only驱动程序。

我是Delphi和C#程序员,所以任何语言的提示都将受到赞赏。

2 个答案:

答案 0 :(得分:5)

您可以发送

~HI

到Zebra打印机,它应该返回它的型号和fw版本

答案 1 :(得分:2)

最近我做了一个小演示。只需在表单上放置TComboBoxTMemo,然后将代码替换为:

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.

编辑:删除了不必要的函数GetDriverNameByOSPrinterName

BTW:在pName中,您拥有驱动程序的名称而不是打印机的名称。 Printername在Windows中是可更改的,因此如果您需要,请使用Printerdrivername。