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

本文关键字:检测 打印机型 Delphi | 更新日期: 2023-09-27 17:47:47

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

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

我是一名Delphi和C#程序员,所以任何语言的提示都将不胜感激。

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

您可以发送

~HI

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

最近我用这个做了一个小演示。只需将TComboBoxTMemo放在Form上,并用以下代码替换代码:

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:删除了不必要的功能GetDriverNameByOSPrinterName

BTW:在pName中,您有驱动程序的名称,而不是打印机的名称。Printername在Windows中是可变的,所以如果你想确定,请使用Printerdrivername。