虚拟打印机实现

本文地址:http://tongxinmao.com/Article/Detail/id/366

所谓的迷你驱动和微软统一驱动有啥不同?

Windows打印体系和打印机驱动的关系。 

不过要澄清的是,一般而言的打印机驱动程序不仅仅包括了打印驱动部分,

还包括了假脱机体系中的打印处理器甚至是端口监视器,

这些一堆DLL文件、GPD文件、Inf文件等等所组成的驱动程序,

而严格意义上来说,打印驱动仅仅包括图形渲染和用户界面两个部分 


其中带方框的部分都是可以编写安装的。

 

所谓的微软统一驱动,就是系统默认的驱动,而迷你驱动就是在此基础上进行个性化定制。

迷你驱动包括GPD文件和资源DLL,

GPD文件就是一个脚本,定义了“打印机首选项”中将出现那些设置,有那些选项可选;

资源DLL就是一个仅仅包含Resource的DLL,提供给GPD使用其中的资源。。

 

UI-Plugin 就是在“打印机首选项”中定义一些特殊的设置页面; 

Render-Plugin则可以对渲染绘制过程进行特殊的处理; 

打印处理器负责SPL文件操作 EMF/RAW 文件操作等。

 

因此,虚拟打印机有两种方式可以实现:

第一种,在打印处理器中做手脚,截取SPL缓冲文件,解析分解成EMF文件;

第二种,在Render-Plugin中做处理,在进行图形渲染的时候将图像绘制到其他地方,例如一个BMP文件。




虚拟打印机在注册表中添加的项

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Print/Environments/Windows NT x86/Print Processors/vprproc 

AutoSave     1为自动保存

DestDir         可以指定路径,如果不存在,系统会创建文件夹和文件

1.下载一个打印驱动.并修改里面的INF文件.


2.修改DDK 里面的例子 genprint, 并使用ddk里的build命令直接build.
  (顺带说一下,执行build程序要先执行Windows XP Checked Build Environment 这样的环境设置BAT)
  修改的地方是:winprint.c 里的 PrintDocumentOnPrintProcessor 函数,将其中 调用PrintEMFJob的地方更改为我们处理的函数.
  一般会将这个函数放在另一个DLL中.
  
3.更改注册表,使其spl文件名根据job id生成.这样我们通过其JOB ID就可以得到相应的SPL文件.
  参考:http://www.undocprint.org/winspool/spool_files#reading_job_data 里面的 Force JobID in Spoolfile names

4.通过spl文件结构,获取EMF文件。

5.把EMF文件转换为其它图片文件。

  
QA:
Q:如何修改打印驱动?我对里面的驱动格式一点都不熟悉。
A:不用改了,我用的是HP LaserJet 4驱动,下载地址:
  http://h50176.www5.hp.com/support/C2001A/more_info_soar_lj631sc.html
   
  以下是inf内容:

////////////////////////////////////inf start///////////////////////////////////////
;Hewlett-Packard LaserJet combined printer driver INF file for Windows XP and 2000
;Copyright Hewlett-Packard 2001
; List of supported printers, manufacturers
[Version]
Signature="$Windows NT$"
Provider=%虚拟打印%
ClassGUID={4D36E979-E325-11CE-BFC1-08002BE10318}
Class=Printer
DriverVer=1/18/2002,02.00.12.02
; Manufacturer section.
;
; This section lists all of the manufacturers
; that we will display in the Dialog box
[Manufacturer]
%虚拟打印%=DefaultInstall

;
; MS Driver Core installs
;
; These sections are used by Printer OEMs to install MS provided drivers
; Installer Sections
;
; These sections control file installation, and reference all files that
; need to be copied. The section name will be assumed to be the driver
; file, unless there is an explicit DriverFile section listed.
;
; Note: Pulling HPLJ4.GPD via Needs= directive to get pcl5eres.dll from XP
;       drivers.cab file due to XP defect whereby this file wasn't listed
;       This also takes care of pjlmon.dll, ttfsub.gpd, UNIDRV sections
;       but unavoidably brings with it the unneeded hplj4.gpd file.
;      
;   In the case of the LJ4 install does Needs=HPLJ4.GPD overwrite the
;   just copied newer version of hplj4.gpd??? In this case perhaps, use
;   Needs=HPLJ4L.GPD to avoid this issue?
;
; Phase II GPDs
[DefaultInstall]
"虚拟打印机"                 = HPLJ4L.GPD.XP        ,Hewlett-PackardLaserC029,HP_LaserJet_4L
;Hewlett-PackardLaserC029是设备的ID
;
; XP
;Old CopyFiles statement before use of Includes= & Needs= ...
;CopyFiles=HPPRPROC,UI,RENDER,@HPLJ4.GPD,@hpc02.ini,RES_COMMON,HELP,UNIDRV,PJLMON.DLL,@TTFSUB.GPD
[HPLJ4L.GPD.XP]
CopyFiles=HPPRPROC,@HPLJ4L.GPD,@hpcstr02.dll
Include=NTPRINT.INF
Needs=HPLJ4.GPD
DataSection=HP_UNIDRV_BIDI_DATA
DataFile=HPLJ4L.GPD
PrintProcessor=%PRINT_PROCESSOR%

; Copy Sections
;
; Lists of files that are actually copied. These sections are referenced
; from the installer sections, above. Only create a section if it contains
; two or more files (if we only copy a single file, identify it in the
; installer section, using the @filename notation) or if it's a color
; profile (since the DestinationDirs can only handle sections, and not
; individual files).

[RES_COMMON]
pcl5eres.dll
hpcstr02.dll

[HPPRPROC]
genprint.dll,,,0x00000020

;
; Data Sections
;
[HP_UNIDRV_DATA]
DriverFile=UNIDRV.DLL
ConfigFile=UNIDRVUI.DLL
HelpFile=UNIDRV.HLP
; Data section that attaches PJLMON as the driver Lang Monitor
[HP_UNIDRV_BIDI_DATA]
DriverFile=UNIDRV.DLL
ConfigFile=UNIDRVUI.DLL
HelpFile=UNIDRV.HLP
LanguageMonitor=%PJL_MONITOR%

;
; Call SetupSetDirectoryId with 66000 to set the target directory at runtime
; (depending on which environment drivers are getting installed)
;
[DestinationDirs]
DefaultDestDir=66000
HPPRPROC=66001
PJLMON.DLL=66002
;
; Control Flags
;
[ControlFlags]
ExcludeFromSelect = HWP20D0, HWP21A0, HWP2140, HWP20F0
;
;  Source Disk Section
;  This was added to enable Web Point&Print.
;  All files added to this must also be added to Layout.INF
;
[SourceDisksNames]
1=%DiskID1%,,,""
[SourceDisksFiles]
;
; MS Files
;
PCL5ERES.DLL = 1
UNIDRV.DLL   = 1
UNIRES.DLL   = 1
UNIDRVUI.DLL = 1
STDNAMES.GPD = 1
UNIDRV.HLP   = 1
TTFSUB.GPD   = 1
;
; Phase II
;
HPCUI02.DLL  = 1
HPCRD02.DLL  = 1
HPCFNT02.DLL = 1
HPCSTR02.DLL = 1
genprint.dll  = 1
HPCLJX02.HLP = 1
HPC02.INI    = 1
HPOEMUI.DLL  = 1
HPCMBOX.INI  = 1
HPLJ4L.GPD   = 1
;
; Localizable Strings
;
[Strings]
PrinterClassName="Printers"
DiskID1="虚拟打印安装盘"
PJL_MONITOR="PJL Language Monitor,PJLMON.DLL"
;PRINT_PROCESSOR="HPPRN02,hpprn02.dll"
PRINT_PROCESSOR="Vprint,genprint.dll"
虚拟打印="虚拟打印"

//////////////////////////inf end//////////////////////////

Q:为什么我在打印的时候, PrintDocumentOnPrintProcessor 没有被调用?
A:可能是因为在你的虚拟打印机设了脱机的原故,当时我就碰到了.
  
Q:如何方便调试.
A:因为genprint.dll是被Print服务占用的,因此,替换的时候需要停止改服务.
  Net Start "Print Spooler"
  Copy file file
  Net Stop  "Print Spooler"

Q:从SPL中分离出EMF文件需要注意什么吗?
A:一定会用到的三个结构 SPL_HEADER(SPL头)、SMR(SPL记录)、ENHMETAHEADER(EMF头).

Q:我不能用acdsee等软件打开分离出的EMF,但为什么用IE是可以打得开的?
  也许你用的是SMR::nSize 来做为EMF的write size
  而不是使用ENHMETAHEADER::nBytes 来做为EMF的write size
  
Q:我想通过GDI+把EMF文件转为BMP,为什么文字部份显视黑屏?
A:不知道,我是用IPicture 读取,并且Render到DC后再保存为BMP的,这样也不行,在Render内存的时候就不行了。
   最后我用的是CXImage 这个库做的转换.

Q:最后我的OFFICE 2007里面的文字打印出来被挤得变形了。
A:嘿嘿嘿,其实我已经放弃了这个方案了。使用OEMDLL里面的Bitmap例子来实现了。

网上的资料:
http://www.undocprint.org                               95%以上的资料来源.
http://www.microsoft.com/india/msdn/articles/130.aspx   有个专门的函数取得相关的SPL文件名.
http://fxh7622.blog.51cto.com/63841/d-3                 最先找到的网址,里面的例子部分是用DELPHI的,我的INF文件就是从这里拷的


Windows的假脱机打印会在Windows/System32/spool/PRINERS目录下生成.spl和.shd文件,其中的打印内

容存贮在.spl文件中,但是.spl文件格式似乎未公开,那么如何才能将未知的.spl文件剥离成.emf文件呢?

              首先,让我们了解一下Windows打印机制:


其中ISV是应用软件接口,IHV是硬件接口,左边是XP的打印模型,右边是Vista最新的XPS打印模

型,但两者可以互相转换,具有良好的兼容性。不过,这里暂时只关心XP系统的打印过程。

基本的思路是: 打印过程发生时,GDI模块和打印驱动(由打印机厂商提供)进行基本的数据交换,在假

脱机设置环境下,生成打印机命令文件:.spl或.emf文件,作为一个打印池的作业,然后Windows后台打印线

程处理打印作业,将数据文件送至打印机打印,打印完删除该打印文件


当定位到0x50的文件位置,读取2个DWORD数据之后,就是.emf文件开始了。.emf文件格式是公开的,而

且非常简单,是一系列EMR_XXX开口结构的紧密排列,通常以EMR_HEADER(0x01)开头,以EMR_EOF

(0x0E)结尾。其实我们根本没有必要去解析.emf文件格式,Windows  SDK有专门显示.emf文件的API,3个函数就

搞定:

                      HENHMETAFILE hEMF = GetEnhMetaFile("EMF_DumpOK.emf");
                      PlayEnhMetaFile (dc.m_hDC, hEMF, &rc) ;   
                      DeleteEnhMetaFile (hEMF) ;

然后.spl文件还有一些东西,我现在还没有解析出来,但是.emf文件已经剥离出来了,后面的可以先不理它。

                  然后,开始写程序喽,因为比较简单,所以代码有点随便哦~~:)


上一篇:LINUX USB gadget
下一篇:tesseract4编译安装C6