另一种选择是使用 WMI。您可以尝试使用 Win32_PrintJob 类(从 Win32_PrintJob 中选择 *)。
使用这样的代码进行测试(使用Rodrigo Ruz的“WMI Delphi 代码创建器”创建)
//-----------------------------------------------------------------------------------------------------
// This code was generated by the Wmi Delphi Code Creator (WDCC) Version 1.8.5.0
// http://code.google.com/p/wmi-delphi-code-creator/
// Blog http://theroadtodelphi.wordpress.com/wmi-delphi-code-creator/
// Author Rodrigo Ruz V. (RRUZ) Copyright (C) 2011-2014
//-----------------------------------------------------------------------------------------------------
//
// LIABILITY DISCLAIMER
// THIS GENERATED CODE IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED.
// YOU USE IT AT YOUR OWN RISK. THE AUTHOR NOT WILL BE LIABLE FOR DATA LOSS,
// DAMAGES AND LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR MISUSING THIS CODE.
//
//----------------------------------------------------------------------------------------------------
program GetWMI_Info;
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX,
ComObj,
Variants,
Dialogs;
// La clase Win32_PrintJob representa un trabajo de impresión generado por una aplicación Win32. Las unidades de trabajo generadas por el comando Imprimir de una aplicación que se ejecuta en un sistema Win32 son descendientes (o miembros) de esta clase.
// Ejemplo: un documento de impresora creado por una aplicación de Office 97
procedure GetWin32_PrintJobInfo;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
wbemFlagForwardOnly = $00000020;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
str:String;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_PrintJob','WQL',wbemFlagForwardOnly);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
Str := '';
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Str := Str + sLineBreak + Format('Caption %s',[String(FWbemObject.Caption)]);// String
Str := Str + sLineBreak + Format('DataType %s',[String(FWbemObject.DataType)]);// String
Str := Str + sLineBreak + Format('Description %s',[String(FWbemObject.Description)]);// String
Str := Str + sLineBreak + Format('Document %s',[String(FWbemObject.Document)]);// String
Str := Str + sLineBreak + Format('DriverName %s',[String(FWbemObject.DriverName)]);// String
Str := Str + sLineBreak + Format('ElapsedTime %s',[String(FWbemObject.ElapsedTime)]);// Datetime
Str := Str + sLineBreak + Format('HostPrintQueue %s',[String(FWbemObject.HostPrintQueue)]);// String
Str := Str + sLineBreak + Format('JobId %d',[Integer(FWbemObject.JobId)]);// Uint32
Str := Str + sLineBreak + Format('JobStatus %s',[String(FWbemObject.JobStatus)]);// String
Str := Str + sLineBreak + Format('Name %s',[String(FWbemObject.Name)]);// String
Str := Str + sLineBreak + Format('Notify %s',[String(FWbemObject.Notify)]);// String
Str := Str + sLineBreak + Format('Owner %s',[String(FWbemObject.Owner)]);// String
Str := Str + sLineBreak + Format('PagesPrinted %d',[Integer(FWbemObject.PagesPrinted)]);// Uint32
Str := Str + sLineBreak + Format('PrintProcessor %s',[String(FWbemObject.PrintProcessor)]);// String
Str := Str + sLineBreak + Format('Priority %d',[Integer(FWbemObject.Priority)]);// Uint32
Str := Str + sLineBreak + Format('Size %d',[Integer(FWbemObject.Size)]);// Uint32
Str := Str + sLineBreak + Format('Status %s',[String(FWbemObject.Status)]);// String
Str := Str + sLineBreak + Format('StatusMask %d',[Integer(FWbemObject.StatusMask)]);// Uint32
Str := Str + sLineBreak + Format('TimeSubmitted %s',[String(FWbemObject.TimeSubmitted)]);// Datetime
Str := Str + sLineBreak + Format('TotalPages %d',[Integer(FWbemObject.TotalPages)]);// Uint32
Str := Str + sLineBreak + '--------------------------------------------------------';
MessageDlg(Str, mtInformation, [mbOK], 0);
FWbemObject:=Unassigned;
end;
end;
begin
try
CoInitialize(nil);
try
GetWin32_PrintJobInfo;
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Writeln('Press Enter to exit');
Readln;
end.
如果您将文件发送到打印机并执行项目(这是使用 Delphi 6 编译的),您可以获得如下结果:

问候。