|
-
May 17th, 2001, 10:03 AM
#1
URGENT: How to purge printer queue programatically?
Hi gurus!
I need to purge printer queue programatically. I'm running VB6 on NT4.0. In MSDN is described API function SetPrinter(...) using which it should work, but doesn't. It returns 0 (which means error). Then after calling GetLastError() return value is 0 (which means no error).
YEAH I KNOW THIS IS MYSTIFICAL SITUATION, BUT THANKS MICRO$OFT WE MEET SIMILAR PROBLEMS ALMOST EVERY DAY ;O(
This is piece of code which I wrote (I found on web few similar codes, but they does'n work for me too). Please try to find what's wrong:
private Const PRINTER_CONTROL_PURGE = 3
private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (byval pPrinterName as string, phPrinter as Long, byval pDefault as Long) as Long
private Declare Function ClosePrinter Lib "winspool.drv" (byval hPrinter as Long) as Long
private Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (byval hPrinter as Long, byval Level as Long, pPrinter as Byte, byval Command as Long) as Long
private Declare Function GetLastError Lib "kernel32" () as Long
private Sub cmdCancelJobsInQueue_Click()
Dim objPrinterTemp as Printer
Dim hPrinter as Long
Dim strPrinterName as string
Dim lngResult as Long
' find name of printer which "sits" on defined port
for Each objPrinterTemp In Printers
If Trim(UCase(objPrinterTemp.Port)) = Trim(UCase("com1:")) then
strPrinterName = objPrinterTemp.DeviceName
set objPrinterTemp = nothing
Exit for
End If
next
' open printer
lngResult = OpenPrinter(strPrinterName, hPrinter, byval 0)
' cancel print jobs
lngResult = SetPrinter(hPrinter, 0, vbNull, PRINTER_CONTROL_PURGE)
If Not lngResult then
MsgBox "SetPrinter returned 0. That means there was an error"
lngResult = GetLastError
MsgBox "GetLastError returned: " & lngResult
End If
' close printer
lngResult = ClosePrinter(hPrinter)
End Sub
Thanks a lot...!!!
Best regards
Marek Slahor
-
May 17th, 2001, 10:22 AM
#2
Re: URGENT: How to purge printer queue programatically?
Try this
'module
Public Const PRINTER_CONTROL_PAUSE = 1
Public Const PRINTER_CONTROL_PURGE = 3
Public Const PRINTER_CONTROL_RESUME = 2
Public Const PRINTER_CONTROL_SET_STATUS = 4
'The PRINTER_DEFAULTS structure holds information about printer settings which
'we will not be using as well as security information which we will need,
'not all users will have the rights to pause or resume printers.
Public Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As Long 'DEVMODE
DesiredAccess As Long
End Type
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const PRINTER_ACCESS_ADMINISTER = &H4
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Public Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, ByVal pPrinter As Long, ByVal Command As Long) As Long
'The three control functions Purge, fResume and Pause are simply calling our
'main control function with a flag for the required operation. I used the name
'fResume as Resume is a reserved keyword in VB.
Public Function Purge(strPrinter As String) As Boolean
Purge = SetPrinterState(strPrinter, PRINTER_CONTROL_PURGE)
End Function
Public Function fResume(strPrinter As String) As Boolean
fResume = SetPrinterState(strPrinter, PRINTER_CONTROL_RESUME)
End Function
Public Function Pause(strPrinter As String) As Boolean
Pause = SetPrinterState(strPrinter, PRINTER_CONTROL_PAUSE)
End Function
'The SetPrinterState function works in three stages, open the printer, set
'the printer state then close the printer.
Private Function SetPrinterState(strPrinter As String, State As Long) As Boolean
Dim hPrinter As Long
Dim pdDefaults As PRINTER_DEFAULTS
'We set the pdDefaults structure with the required access for this operation,
'the data type is set to RAW although for this operation the value doesn't
'matter. pDevMode is a pointer to another structure which again is not
'required so we set it to zero.
pdDefaults.DesiredAccess = PRINTER_ALL_ACCESS
pdDefaults.pDatatype = "RAW"
pdDefaults.pDevMode = 0
'The OpenPrinter API call requires the name of the printer, a variable to hold
'the returned handle and the printer defaults we set previously. The handle
'is allocated until we close the printer and is used in further calls to
'access the printer. If the call fails either because the user doesn't have
'the correct rights or the printer doesn't exist the return value is 0 and we
'exit our function.
If OpenPrinter(strPrinter, hPrinter, pdDefaults) = 0 Then
SetPrinterState = False
Exit Function
End If
'SetPrinter changes the state of the printer according to the State parameter
'again returning 0 if it fails.
If SetPrinter(hPrinter, 0, 0, State) = 0 Then
ClosePrinter hPrinter
SetPrinterState = False
Exit Function
End If
'Finally we close the printer which releases the handle. If we were to fail to
'close the printer our application would leak handles which would eventually
'lead to Windows becoming unstable.
ClosePrinter hPrinter
End Function
'form
Private Sub cmdPause_Click()
Call Pause("HP LaserJet 5Si")
End Sub
Private Sub cmdPurge_Click()
Call Purge("HP LaserJet 5Si")
End Sub
Private Sub cmdResume_Click()
Call fResume("HP LaserJet 5Si")
End Sub
Iouri Boutchkine
[email protected]
-
May 17th, 2001, 11:01 AM
#3
Re: URGENT: How to purge printer queue programatically?
WOW MAN, IT REALLY WORKS...!!!
THANK YOU VERY MUCH INDEED!
Best regards
Marek Slahor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|