1
2 //this code shows how to clean out the printer spooler
3 uses WinSpool, Windows
4
5 procedure ClearPrinterSpooler( iPrinterName : string );
6 const
7 Defaults: TPrinterDefaults = (
8 pDatatype : nil;
9 pDevMode : nil;
10 DesiredAccess : PRINTER_ACCESS_USE or PRINTER_ACCESS_ADMINISTER );
11
12 var PrinterHandle : THandle;
13
14 function GetJobCount: LongWord;
15 type
16 TPrinterInfos = array[0..0] of _PRINTER_INFO_2; //TPrinterInfo - Typecasting
17 var
18 vpPrintEnum : pointer;
19 vPI : _PRINTER_INFO_2;
20 vHGlobal : cardinal;
21 vLevel : DWORD;
22 vNeeded : DWORD;
23 vReturned : DWORD;
24 vFlag : BOOLEAN;
25 vIdx : dword;
26 begin
27 {$R-}
28 vpPrintEnum := nil;
29 vHGlobal := 0;
30 try
31 vLevel := 2;
32 result := 0;
33 EnumPrinters(PRINTER_ENUM_LOCAL, nil, vLevel, vpPrintEnum, 0, vNeeded,
34 vReturned);
35 if (vNeeded = 0) then exit;
36
37 // allocate memory
38 vHGlobal := GlobalAlloc(GHND, vNeeded);
39 vpPrintEnum := GlobalLock(vHGlobal);
40 if (vpPrintEnum = nil) then exit;
41
42 vFlag := EnumPrinters(PRINTER_ENUM_LOCAL, PChar(iPrinterName), vLevel,
43 vpPrintEnum, vNeeded,
44 vNeeded, vReturned);
45 if (not vFlag) then exit;
46
47 for vIdx := 0 to vReturned-1 do
48 begin
49 vPI := TPrinterInfos(vpPrintEnum^)[vIdx];
50 if ( Uppercase( vPI.pPrinterName) = Uppercase( iPrinterName ) )then
51 begin
52 result := vPI.cJobs;
53 break;
54 end;
55 end;
56 finally
57 if (vpPrintEnum <> nil) then GlobalUnlock(vHGlobal);
58 if (vHGlobal <> null) then GlobalFree(vHGlobal);
59 end;
60 {$R+}
61 end;
62
63 begin
64 if not OpenPrinter(PChar( iPrinterName ), PrinterHandle, @Defaults) then
65 GetLastError;
66 try
67 //if spooler have job then clean it.
68 if GetJobCount > 0 then begin
69 if not WinSpool.SetPrinter( PrinterHandle, 0, nil, PRINTER_CONTROL_PURGE )
70 then
71 GetLastError;
72 end;
73 finally
74 ClosePrinter( PrinterHandle );
75 end;
76 end;
|