Get number of jobs in print queue

The number of jobs in print queue can be retrieved by simplified version of Enumerating jobs in print queue


$SAMPLECODE$

1
2
3
4
5
6
7
* WMI
* Default VFP printer
lcPrinterName = SET("Printer",3) 
? GetNumberOfJobs(lcPrinterName)
RETURN

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

FUNCTION GetNumberOfJobs(tcPrinterName)
LOCAL lnNumberOfJobs

loLocator 	= CREATEOBJECT('WBEMScripting.SWBEMLocator')
loWMI		= loLocator.ConnectServer() 
loWMI.Security_.ImpersonationLevel = 3  		&& Impersonate
lcPrinterName = STRTRAN(tcPrinterName , "\", "\\")   

loPrintJobs =  loWMI.ExecQuery("Select * from Win32_PrintJob " + ;
				"  WHERE Name LIKE '" + lcPrinterName + "%'")

TRY				
	lnNumberOfJobs = loPrintJobs.Count
CATCH TO loErr
	? "Error: ", loErr.Errorno, loErr.Message
	lnNumberOfJobs = -1
ENDTRY
RETURN lnNumberOfJobs


The WinApiErrMsg function in the code below is from Retrieving Windows system error message

.

1
2
3
4
5
6
7
* Windows API
* Default VFP printer
lcPrinterName = SET("Printer",3) 
? GetNumberOfJobs(lcPrinterName)
RETURN

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

FUNCTION GetNumberOfJobs(tcPrinterName)
LOCAL lnOK, lcErrorTxt, lnHandle, lnNumberOfJobs, lnNeeded, lcBuffer, lcTab, lcCrLf

DECLARE Long GetLastError IN kernel32.dll 
lcCrLf = CHR(13) + CHR(10)
lcTab = CHR(9)
lnHandle = 0

* Open a Pinter
lnOK = OpenPrinter( tcPrinterName, @lnHandle, NULL) 
IF lnOK = 0 THEN 
	lcErrorTxt = WinApiErrMsg(GetLastError())
	? "Cannot Open Printer " + tcPrinterName + lcCrLf + lcTab + lcErrorTxt
	RETURN -1
ENDIF

lnNeeded = 0
lnNumberOfJobs = 0

* Get the size of the buffer in lnNeeded 
lnOK = EnumJobsCountOnly( lnHandle, 0, 127, 1, NULL, 0, @lnNeeded, @lnNumberOfJobs  )
IF lnOK = 0 THEN 
	IF GetLastError() <> 122   && The buffer too small error
		lcErrorTxt = WinApiErrMsg(GetLastError())
		? "Error enumerating Print Jobs for " + tcPrinterName + lcCrLf + lcTab + lcErrorTxt
		= ClosePrinter( lnHandle )
		RETURN -1
	ENDIF	
ENDIF	

IF lnNeeded = 0
	RETURN 0
ENDIF	

* Allocate the buffer of double size in case # jobs have been added in between
lcBuffer = REPLICATE( CHR(0), lnNeeded*2)
* Get the number of jobs
lnOK = EnumJobsCountOnly( lnHandle, 0, 127, 1, @lcBuffer, @lnNeeded, @lnNeeded, @lnNumberOfJobs  )
IF lnOK = 0 THEN 
	lcErrorTxt = WinApiErrMsg(GetLastError())
	? "Error enumerating PrintJobs for " + tcPrinterName + lcCrLf + lcTab + lcErrorTxt
	= ClosePrinter( lnHandle )
	RETURN -1
ENDIF	

= ClosePrinter( lnHandle )
RETURN lnNumberOfJobs
*----------------------------------------------------------------------------------------------

FUNCTION OpenPrinter(tcPrinterName, thPrinter, tcDefault)
DECLARE Long OpenPrinter IN WinSpool.Drv ;
	String pPrinterName, Long@ phPrinter, String pDefault
RETURN 	OpenPrinter(tcPrinterName, @thPrinter, tcDefault)

FUNCTION ClosePrinter (thPrinter)
DECLARE Long ClosePrinter IN WinSpool.Drv Long hPrinter
RETURN ClosePrinter(thPrinter)

FUNCTION EnumJobsCountOnly(thPrinter, tnFirstJob, tnNoJobs, tnLevel, ;
	tnJob, tnBuf, tnNeeded, tnReturned)

DECLARE Long EnumJobs IN WinSpool.Drv AS EnumJobsCountOnly ;
	Long hPrinter, Long FirstJob, Long NoJobs, Long Level, ;
	String pJob, Long cbBuf, Long @pcbNeeded, Long @pcReturned
RETURN EnumJobsCountOnly(thPrinter, tnFirstJob, tnNoJobs, tnLevel, ;
	tnJob, @tnBuf, @tnNeeded, @tnReturned)


Comments

thank's, it's very fine and does work good!