windows 是如何识别连接的打印机并识别出其品牌和型号的?
用代码如何实现?

解决方案 »

  1.   

    驱动啊,windows已经做好了直接取就行了,可以参考下打印对话框的VCL源码。
      

  2.   

    获取当前打印机的名称、驱动程序、打印端口信息:
    以下是来自Borland公司的完整例子。请注意其中两条判断语句,这说明有时候仅仅凭getprinters并不能保证信息的正确,还需要WIN.INI的帮助。
    uses printers;
    {$IFNDEF WIN32}
    const MAX_PATH = 144;
    {ENDIF}
    procedure TForm1.Button1Click(Sender : TObject);
    var 
     pDevice : pChar;
       pDriver : pChar;
     pPort   :  pChar;
     hDMode : THandle;
    begin
     if PrintDialog1.Execute then begin
       GetMem(pDevice,cchDeviceName);
       GetMem(pDriver,MAX_PATH);
       GetMem(pPort,MAX_PATH);
       Printer.GetPrinter(pDevice,pDriver,pPort,hDMode);
       if lStrLen(pDriver) = 0 then begin
                 GetProfileString('Devices',pDevice,'',pDriver,MAX_PATH);
           pDriver[pos(',',pDriver) - 1] := #0;
       end;
       if lStrLen(pPort) = 0 then begin
           GetProfileString('Devices',pDevice,'',pPort,MAX_PATH);
           lStrCpy(pPort,@pPort[lStrLen(pPort) + 2]);
       end;
       FreeMem(pDevice,cchDeviceName);
       FreeMem(pDriver,MAX_PATH);
       FreeMem(pPort,MAX_PATH);
     end;
    end;
      

  3.   

    在Windows的Win.ini文件中有下面的一些配置信息:
    [Windows]
    load = 
    run =
    NullPort = None
    device = HPLaserJetIII,HPPCL5MS,LPT1
    .............
        其中Windows节的device键中指明了当前系统中默认打印机的信息。可以通过WindowsAPI函数GetProfileString获取该信息。函数GetProfileString的原形如下:
     DWORDGetProfileString(
     LPCTSTRlpAppName,  //指定节名的字符串
     LPCTSTRlpKeyName,  //指定键名的字符串
     LPCTSTRlpDefault,    //没有找到键名时返回的字符串
     LPTSTRlpReturnedString,  //在键名找到时返回的字符串
     DWORDnSize    //lpReturnedString的字节数
     );