Reply to comment

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

This is sample code. Add error handling and adjust to your requirements as necessary.

* WMI
* Default VFP printer
lcPrinterName = SET("Printer",3) 
? GetNumberOfJobs(lcPrinterName)
RETURN
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.

* Windows API
* Default VFP printer
lcPrinterName = SET("Printer",3) 
? GetNumberOfJobs(lcPrinterName)
RETURN
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)

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.