一个class: Option ExplicitPrivate Const CLSNAME = "ApiPrintJob" Public Enum PrintJobStatuses JOB_STATUS_PAUSED = &H1 JOB_STATUS_ERROR = &H2 JOB_STATUS_DELETING = &H4 JOB_STATUS_SPOOLING = &H8 JOB_STATUS_PRINTING = &H10 JOB_STATUS_OFFLINE = &H20 JOB_STATUS_PAPEROUT = &H40 JOB_STATUS_PRINTER = &H80 JOB_STATUS_DELETED = &H100 JOB_STATUS_BLOCKED_DEVICEQUEUE = &H200 JOB_STATUS_USER_INTERVENTION = &H400 JOB_STATUS_RESTART = &H800 End EnumPrivate Type SYSTEMTIME wYear As Integer wMonth As Integer wDayOfWeek As Integer wDay As Integer wHour As Integer wMinute As Integer wSecond As Integer wMilliseconds As Integer End TypePrivate Type JOB_INFO_1 JobId As Long lpPrinterName As String lpMachinename As String lpUserName As String lpDocumentName As String lpDataType As String lpStatus As String Status As PrintJobStatuses Priority As Long Position As Long TotalPages As Long PagesPrinted As Long Submitted As SYSTEMTIME End TypePrivate Type JOB_INFO_2 JobId As Long lpPrinterName As String lpMachinename As String lpUserName As String lpDocumentName As String lpNotifyName As String lpDataType As String lpPrintProcessor As String lpParameters As String lpDriverName As String lpDevMode As Long 'Pointer to DevMode lpStatus As String lpSecurityDescriptor As Long 'Pointer to SECURITY_DESCRIPTOR Status As PrintJobStatuses Priority As Long Position As Long StartTime As Long UntilTime As Long TotalPages As Long JobSize As Long Submitted As SYSTEMTIME time As Long PagesPrinted As Long End Type'\\ Included in WINNT4 and beyond Private Type JOB_INFO_3 JobId As Long NextJobId As Long Reserved As Long 'must be set to zero End TypePrivate Declare Function GetJob Lib "winspool.drv" Alias "GetJobA" _ (ByVal hPrinter As Long, _ ByVal JobId As Long, _ ByVal Level As Long, _ buffer As Long, _ ByVal pbSize As Long, _ pbSizeNeeded As Long) As LongPrivate Declare Function SetJob Lib "winspool.drv" Alias _ "SetJobA" (ByVal hPrinter As Long, _ ByVal JobId As Long, _ ByVal Level As Long, _ pJob As Long, _ ByVal Command As Long) As Long '\\ Private member variables Private mJobId As Long 'Unique id of this job Private mhPrinter As Long 'Printer this job is on...Private mJOB_INFO_1 As JOB_INFO_1 Private mJOB_INFO_2 As JOB_INFO_2 Private mJOB_INFO_3 As JOB_INFO_3'\\ Private variable to prevent unneccessary calls to the dll... Private mLastApiCall(1 To 3) As DatePrivate bChanged As BooleanPublic Enum PrintJobControlCommands JOB_CONTROL_PAUSE = 1 JOB_CONTROL_RESUME = 2 JOB_CONTROL_CANCEL = 3 JOB_CONTROL_RESTART = 4 JOB_CONTROL_DELETE = 5 JOB_CONTROL_SENT_TO_PRINTER = 6 JOB_CONTROL_LAST_PAGE_EJECTED = 7 End Enum'\\ When changing information other than the position for JOB_INFO_1 and '\\ JOB_INFO_2 you must set the Position member to JOB_POSITION_UNSPECIFIED Private Const JOB_POSITION_UNSPECIFIED = 0Private buffer() As Long Private WithEvents dmThis As ApiDeviceModePrivate Sub dmThis_Changed()Call RefreshJobInfo(2) If dmThis.CopyToPointer(mJOB_INFO_2.lpDevMode) Then Call SavePrintJobInfo(2) End IfEnd Sub Public Property Get DeviceMode() As ApiDeviceModeSet dmThis = New ApiDeviceModeCall RefreshJobInfo(2) If dmThis.CreateFromPointer(mJOB_INFO_2.lpDevMode) Then Set DeviceMode = dmThis End IfEnd Property Public Sub CancelJob()Dim lret As LongIf APIDispenser.System.WindowsOSVersion < ver_win_nt4 Then lret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_CANCEL) Else lret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_DELETE) End If If Err.LastDllError Then ReportError Err.LastDllError, CLSNAME & ":CancelJob", GetLastSystemError End IfEnd SubFriend Property Get Changed() As Boolean Changed = bChanged End Property Friend Property Let Changed(ByVal newChanged As Boolean) bChanged = newChanged End Property Public Property Get DataType() As StringIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) DataType = mJOB_INFO_1.lpDataType End IfEnd Property'\\ --[DocumentName]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get DocumentName() As StringIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) DocumentName = mJOB_INFO_1.lpDocumentName End IfEnd Property Friend Property Let JobId(ByVal newJobId As Long) mJobId = newJobIdEnd PropertyPublic Property Get JobId() As Long JobId = mJobIdEnd PropertyPublic Sub LinkToJob(ByVal newJob As ApiPrintJob)If newJob.JobId <> JobId Then With mJOB_INFO_3 .JobId = JobId .NextJobId = newJob.JobId .Reserved = 0 End With Call SavePrintJobInfo(3) End IfEnd Sub'\\ --[MachineName]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get MachineName() As StringIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) MachineName = mJOB_INFO_1.lpMachinename End IfEnd PropertyPublic Property Get NotifyUser() As StringCall RefreshJobInfo(2) NotifyUser = mJOB_INFO_2.lpNotifyNameEnd Property'\\ --[Status]----------------------------------------------------------------------------------------------- ' #--------------------------------------------------------------------------------------------------------- Public Property Get PagesPrinted() As LongIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) PagesPrinted = mJOB_INFO_1.PagesPrinted End IfEnd PropertyPublic Sub PauseJob()Dim lret As Longlret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_PAUSE) If Err.LastDllError Then ReportError Err.LastDllError, CLSNAME & ":PauseJob", GetLastSystemError End IfEnd Sub '\\ --[Status]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get Position() As LongIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) Position = mJOB_INFO_1.Position End IfEnd Property
'续上 Friend Property Let PrinterHandle(ByVal newhPrinter As Long) mhPrinter = newhPrinterEnd PropertyPublic Property Get PrinterName() As StringIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) PrinterName = mJOB_INFO_1.lpPrinterName End IfEnd PropertyPublic Property Get Priority() As LongIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) Priority = mJOB_INFO_1.Priority End IfEnd PropertyPrivate Sub RefreshJobInfo(ByVal Index As Integer)Dim lret As Long Dim SizeNeeded As Long '\\ Prevent unneccesary call to the API If DateDiff("s", mLastApiCall(Index), Now) > 1 Then mLastApiCall(Index) = Now ReDim Preserve buffer(0 To 1) As Long lret = GetJob(mhPrinter, mJobId, Index, buffer(0), UBound(buffer), SizeNeeded) If SizeNeeded > 0 Then ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long lret = GetJob(mhPrinter, mJobId, Index, buffer(0), UBound(buffer) * 4, SizeNeeded) If Index < 1 Or Index > 3 Then Debug.Print "Error in call to ApiPrintJob:RefreshJobInfo - invalid index" Else Select Case Index Case 1 With mJOB_INFO_1 .JobId = buffer(0) .lpPrinterName = StringFromPointer(buffer(1), 1024) .lpMachinename = StringFromPointer(buffer(2), 1024) .lpUserName = StringFromPointer(buffer(3), 1024) .lpDocumentName = StringFromPointer(buffer(4), 1024) .lpDataType = StringFromPointer(buffer(5), 1024) .lpStatus = StringFromPointer(buffer(6), 1024) .Status = buffer(7) .Priority = buffer(8) .Position = buffer(9) .TotalPages = buffer(10) .PagesPrinted = buffer(11) 'Submitted is also here... With .Submitted .wYear = LoWord(buffer(12)) .wMonth = HiWord(buffer(12)) .wDayOfWeek = LoWord(buffer(13)) .wDay = HiWord(buffer(13)) .wHour = LoWord(buffer(14)) .wMinute = HiWord(buffer(14)) .wSecond = LoWord(buffer(15)) .wMilliseconds = HiWord(buffer(15)) End With End With Case 2 With mJOB_INFO_2 .JobId = buffer(0) .lpPrinterName = StringFromPointer(buffer(1), 1024) .lpMachinename = StringFromPointer(buffer(2), 1024) .lpUserName = StringFromPointer(buffer(3), 1024) .lpDocumentName = StringFromPointer(buffer(4), 1024) .lpNotifyName = StringFromPointer(buffer(5), 1024) .lpDataType = StringFromPointer(buffer(6), 1024) .lpPrintProcessor = StringFromPointer(buffer(7), 1024) .lpParameters = StringFromPointer(buffer(8), 1024) .lpDriverName = StringFromPointer(buffer(9), 1024) .lpDevMode = buffer(10) 'To do: Replace with ApiDevMode class when available .lpStatus = StringFromPointer(buffer(11), 1024) .lpSecurityDescriptor = buffer(12) 'To do: Replace... .Status = buffer(13) .Priority = buffer(14) .Position = buffer(15) .StartTime = buffer(16) .UntilTime = buffer(17) .TotalPages = buffer(18) .JobSize = buffer(19) 'Submitted is here.... '4 long values, packed with 8 integers With .Submitted .wYear = LoWord(buffer(20)) .wMonth = HiWord(buffer(20)) .wDayOfWeek = LoWord(buffer(21)) .wDay = HiWord(buffer(21)) .wHour = LoWord(buffer(22)) .wMinute = HiWord(buffer(22)) .wSecond = LoWord(buffer(23)) .wMilliseconds = HiWord(buffer(23)) End With .time = buffer(24) .PagesPrinted = buffer(25) End With Case 3 With mJOB_INFO_3 .JobId = buffer(0) .NextJobId = buffer(1) .Reserved = buffer(2) End With End Select End If End If End IfEnd SubPublic Sub ResumeJob()Dim lret As Longlret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_RESUME) If Err.LastDllError Then ReportError Err.LastDllError, CLSNAME & ":ResumeJob", GetLastSystemError End IfEnd SubPrivate Sub SavePrintJobInfo(ByVal Level As Long)Dim lret As LongSelect Case Level Case 1 '\\ Overwrite existing buffer data with that held locally in JOB_INFO_1 With mJOB_INFO_1 buffer(0) = .JobId '\\ This cannot change and is ignored buffer(1) = LPCSTR(.lpPrinterName) '\\ This is ignored and cannot be changed buffer(2) = LPCSTR(.lpMachinename) '\\ also ignored buffer(3) = LPCSTR(.lpUserName) buffer(4) = LPCSTR(.lpDocumentName) buffer(5) = LPCSTR(.lpDataType) buffer(6) = LPCSTR(.lpStatus) buffer(7) = .Status buffer(8) = .Priority buffer(9) = .Position buffer(10) = .TotalPages buffer(11) = .PagesPrinted With .Submitted '\\ Also ignored buffer(12) = MakeLong(.wYear, .wMonth) buffer(13) = MakeLong(.wDayOfWeek, .wDay) buffer(14) = MakeLong(.wHour, .wMinute) buffer(15) = MakeLong(.wSecond, .wMilliseconds) End With End WithCase 2 '\\ Overwrite existing buffer data with that held locally in JOB_INFO_2 With mJOB_INFO_2 buffer(0) = .JobId '\\ This cannot change and is ignored buffer(1) = LPCSTR(.lpPrinterName) '\\ This is ignored and cannot be changed buffer(2) = LPCSTR(.lpMachinename) '\\ also ignored buffer(3) = LPCSTR(.lpUserName) buffer(4) = LPCSTR(.lpDocumentName) buffer(5) = LPCSTR(.lpNotifyName) buffer(6) = LPCSTR(.lpDataType) buffer(7) = LPCSTR(.lpPrintProcessor) buffer(8) = LPCSTR(.lpParameters) buffer(9) = LPCSTR(.lpDriverName) buffer(10) = .lpDevMode buffer(11) = LPCSTR(.lpStatus) buffer(12) = .lpSecurityDescriptor buffer(13) = .Status buffer(14) = .Priority buffer(15) = .Position buffer(16) = .StartTime buffer(17) = .UntilTime buffer(18) = .TotalPages buffer(19) = .JobSize With .Submitted '\\ Also ignored buffer(20) = MakeLong(.wYear, .wMonth) buffer(21) = MakeLong(.wDayOfWeek, .wDay) buffer(22) = MakeLong(.wHour, .wMinute) buffer(23) = MakeLong(.wSecond, .wMilliseconds) End With buffer(24) = .time buffer(25) = .PagesPrinted End WithCase 3 '\\ Overwrite existing buffer data with that held locally in JOB_INFO_3 With mJOB_INFO_3 buffer(0) = .JobId buffer(1) = .NextJobId buffer(2) = .Reserved End WithEnd Selectlret = SetJob(mhPrinter, mJobId, Level, buffer(0), 0) If Err.LastDllError Then ReportError Err.LastDllError, CLSNAME & ":SavePrintJobInfo", GetLastSystemError Else '\\ Free up "long pointer to string" strings... Call ResetLPCSTRIndexes End IfEnd Sub
'\\ --[Status]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get Status() As PrintJobStatusesIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) Status = mJOB_INFO_1.Status End IfEnd Property '\\ --[Submitted]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get Submitted() As APISystemTimeDim oSystemTime As APISystemTimeCall RefreshJobInfo(1)Set oSystemTime = New APISystemTime oSystemTime.CreateFromPointer (VarPtr(mJOB_INFO_1.Submitted))Set Submitted = oSystemTimeEnd Property'\\ --[Status]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get TotalPages() As LongIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) TotalPages = mJOB_INFO_1.TotalPages End IfEnd Property'\\ --[StartTime]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get StartTime() As LongCall RefreshJobInfo(2) StartTime = mJOB_INFO_2.StartTimeEnd PropertyPublic Property Get UntilTime() As LongCall RefreshJobInfo(2) UntilTime = mJOB_INFO_2.UntilTimeEnd Property Public Property Let Username(ByVal newname As String)Call RefreshJobInfo(1) mJOB_INFO_1.lpUserName = newname mJOB_INFO_1.Position = JOB_POSITION_UNSPECIFIED Call SavePrintJobInfo(1)End Property'\\ --------------------------------------------------------------------------------------------------------- Public Property Get Username() As String Call RefreshJobInfo(1) Username = mJOB_INFO_1.lpUserNameEnd Property
'\\ --[Status]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get Status() As PrintJobStatusesIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) Status = mJOB_INFO_1.Status End IfEnd Property '\\ --[Submitted]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get Submitted() As APISystemTimeDim oSystemTime As APISystemTimeCall RefreshJobInfo(1)Set oSystemTime = New APISystemTime oSystemTime.CreateFromPointer (VarPtr(mJOB_INFO_1.Submitted))Set Submitted = oSystemTimeEnd Property'\\ --[Status]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get TotalPages() As LongIf mhPrinter <> 0 And mJobId <> 0 Then Call RefreshJobInfo(1) TotalPages = mJOB_INFO_1.TotalPages End IfEnd Property'\\ --[StartTime]----------------------------------------------------------------------------------------------- '\\ --------------------------------------------------------------------------------------------------------- Public Property Get StartTime() As LongCall RefreshJobInfo(2) StartTime = mJOB_INFO_2.StartTimeEnd PropertyPublic Property Get UntilTime() As LongCall RefreshJobInfo(2) UntilTime = mJOB_INFO_2.UntilTimeEnd Property Public Property Let Username(ByVal newname As String)Call RefreshJobInfo(1) mJOB_INFO_1.lpUserName = newname mJOB_INFO_1.Position = JOB_POSITION_UNSPECIFIED Call SavePrintJobInfo(1)End Property'\\ --------------------------------------------------------------------------------------------------------- Public Property Get Username() As String Call RefreshJobInfo(1) Username = mJOB_INFO_1.lpUserNameEnd Property
http://www.mvps.org/vb/code/PrnInfo.zip
Option ExplicitPrivate Const CLSNAME = "ApiPrintJob"
Public Enum PrintJobStatuses
JOB_STATUS_PAUSED = &H1
JOB_STATUS_ERROR = &H2
JOB_STATUS_DELETING = &H4
JOB_STATUS_SPOOLING = &H8
JOB_STATUS_PRINTING = &H10
JOB_STATUS_OFFLINE = &H20
JOB_STATUS_PAPEROUT = &H40
JOB_STATUS_PRINTER = &H80
JOB_STATUS_DELETED = &H100
JOB_STATUS_BLOCKED_DEVICEQUEUE = &H200
JOB_STATUS_USER_INTERVENTION = &H400
JOB_STATUS_RESTART = &H800
End EnumPrivate Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End TypePrivate Type JOB_INFO_1
JobId As Long
lpPrinterName As String
lpMachinename As String
lpUserName As String
lpDocumentName As String
lpDataType As String
lpStatus As String
Status As PrintJobStatuses
Priority As Long
Position As Long
TotalPages As Long
PagesPrinted As Long
Submitted As SYSTEMTIME
End TypePrivate Type JOB_INFO_2
JobId As Long
lpPrinterName As String
lpMachinename As String
lpUserName As String
lpDocumentName As String
lpNotifyName As String
lpDataType As String
lpPrintProcessor As String
lpParameters As String
lpDriverName As String
lpDevMode As Long 'Pointer to DevMode
lpStatus As String
lpSecurityDescriptor As Long 'Pointer to SECURITY_DESCRIPTOR
Status As PrintJobStatuses
Priority As Long
Position As Long
StartTime As Long
UntilTime As Long
TotalPages As Long
JobSize As Long
Submitted As SYSTEMTIME
time As Long
PagesPrinted As Long
End Type'\\ Included in WINNT4 and beyond
Private Type JOB_INFO_3
JobId As Long
NextJobId As Long
Reserved As Long 'must be set to zero
End TypePrivate Declare Function GetJob Lib "winspool.drv" Alias "GetJobA" _
(ByVal hPrinter As Long, _
ByVal JobId As Long, _
ByVal Level As Long, _
buffer As Long, _
ByVal pbSize As Long, _
pbSizeNeeded As Long) As LongPrivate Declare Function SetJob Lib "winspool.drv" Alias _
"SetJobA" (ByVal hPrinter As Long, _
ByVal JobId As Long, _
ByVal Level As Long, _
pJob As Long, _
ByVal Command As Long) As Long
'\\ Private member variables
Private mJobId As Long 'Unique id of this job
Private mhPrinter As Long 'Printer this job is on...Private mJOB_INFO_1 As JOB_INFO_1
Private mJOB_INFO_2 As JOB_INFO_2
Private mJOB_INFO_3 As JOB_INFO_3'\\ Private variable to prevent unneccessary calls to the dll...
Private mLastApiCall(1 To 3) As DatePrivate bChanged As BooleanPublic Enum PrintJobControlCommands
JOB_CONTROL_PAUSE = 1
JOB_CONTROL_RESUME = 2
JOB_CONTROL_CANCEL = 3
JOB_CONTROL_RESTART = 4
JOB_CONTROL_DELETE = 5
JOB_CONTROL_SENT_TO_PRINTER = 6
JOB_CONTROL_LAST_PAGE_EJECTED = 7
End Enum'\\ When changing information other than the position for JOB_INFO_1 and
'\\ JOB_INFO_2 you must set the Position member to JOB_POSITION_UNSPECIFIED
Private Const JOB_POSITION_UNSPECIFIED = 0Private buffer() As Long
Private WithEvents dmThis As ApiDeviceModePrivate Sub dmThis_Changed()Call RefreshJobInfo(2)
If dmThis.CopyToPointer(mJOB_INFO_2.lpDevMode) Then
Call SavePrintJobInfo(2)
End IfEnd Sub
Public Property Get DeviceMode() As ApiDeviceModeSet dmThis = New ApiDeviceModeCall RefreshJobInfo(2)
If dmThis.CreateFromPointer(mJOB_INFO_2.lpDevMode) Then
Set DeviceMode = dmThis
End IfEnd Property
Public Sub CancelJob()Dim lret As LongIf APIDispenser.System.WindowsOSVersion < ver_win_nt4 Then
lret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_CANCEL)
Else
lret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_DELETE)
End If
If Err.LastDllError Then
ReportError Err.LastDllError, CLSNAME & ":CancelJob", GetLastSystemError
End IfEnd SubFriend Property Get Changed() As Boolean
Changed = bChanged
End Property
Friend Property Let Changed(ByVal newChanged As Boolean)
bChanged = newChanged
End Property
Public Property Get DataType() As StringIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
DataType = mJOB_INFO_1.lpDataType
End IfEnd Property'\\ --[DocumentName]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get DocumentName() As StringIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
DocumentName = mJOB_INFO_1.lpDocumentName
End IfEnd Property
Friend Property Let JobId(ByVal newJobId As Long) mJobId = newJobIdEnd PropertyPublic Property Get JobId() As Long JobId = mJobIdEnd PropertyPublic Sub LinkToJob(ByVal newJob As ApiPrintJob)If newJob.JobId <> JobId Then
With mJOB_INFO_3
.JobId = JobId
.NextJobId = newJob.JobId
.Reserved = 0
End With
Call SavePrintJobInfo(3)
End IfEnd Sub'\\ --[MachineName]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get MachineName() As StringIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
MachineName = mJOB_INFO_1.lpMachinename
End IfEnd PropertyPublic Property Get NotifyUser() As StringCall RefreshJobInfo(2)
NotifyUser = mJOB_INFO_2.lpNotifyNameEnd Property'\\ --[Status]-----------------------------------------------------------------------------------------------
' #---------------------------------------------------------------------------------------------------------
Public Property Get PagesPrinted() As LongIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
PagesPrinted = mJOB_INFO_1.PagesPrinted
End IfEnd PropertyPublic Sub PauseJob()Dim lret As Longlret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_PAUSE)
If Err.LastDllError Then
ReportError Err.LastDllError, CLSNAME & ":PauseJob", GetLastSystemError
End IfEnd Sub
'\\ --[Status]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get Position() As LongIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
Position = mJOB_INFO_1.Position
End IfEnd Property
Friend Property Let PrinterHandle(ByVal newhPrinter As Long) mhPrinter = newhPrinterEnd PropertyPublic Property Get PrinterName() As StringIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
PrinterName = mJOB_INFO_1.lpPrinterName
End IfEnd PropertyPublic Property Get Priority() As LongIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
Priority = mJOB_INFO_1.Priority
End IfEnd PropertyPrivate Sub RefreshJobInfo(ByVal Index As Integer)Dim lret As Long
Dim SizeNeeded As Long
'\\ Prevent unneccesary call to the API
If DateDiff("s", mLastApiCall(Index), Now) > 1 Then
mLastApiCall(Index) = Now ReDim Preserve buffer(0 To 1) As Long
lret = GetJob(mhPrinter, mJobId, Index, buffer(0), UBound(buffer), SizeNeeded) If SizeNeeded > 0 Then
ReDim Preserve buffer(0 To (SizeNeeded / 4) + 3) As Long
lret = GetJob(mhPrinter, mJobId, Index, buffer(0), UBound(buffer) * 4, SizeNeeded) If Index < 1 Or Index > 3 Then
Debug.Print "Error in call to ApiPrintJob:RefreshJobInfo - invalid index"
Else
Select Case Index
Case 1
With mJOB_INFO_1
.JobId = buffer(0)
.lpPrinterName = StringFromPointer(buffer(1), 1024)
.lpMachinename = StringFromPointer(buffer(2), 1024)
.lpUserName = StringFromPointer(buffer(3), 1024)
.lpDocumentName = StringFromPointer(buffer(4), 1024)
.lpDataType = StringFromPointer(buffer(5), 1024)
.lpStatus = StringFromPointer(buffer(6), 1024)
.Status = buffer(7)
.Priority = buffer(8)
.Position = buffer(9)
.TotalPages = buffer(10)
.PagesPrinted = buffer(11)
'Submitted is also here...
With .Submitted
.wYear = LoWord(buffer(12))
.wMonth = HiWord(buffer(12))
.wDayOfWeek = LoWord(buffer(13))
.wDay = HiWord(buffer(13))
.wHour = LoWord(buffer(14))
.wMinute = HiWord(buffer(14))
.wSecond = LoWord(buffer(15))
.wMilliseconds = HiWord(buffer(15))
End With
End With
Case 2
With mJOB_INFO_2
.JobId = buffer(0)
.lpPrinterName = StringFromPointer(buffer(1), 1024)
.lpMachinename = StringFromPointer(buffer(2), 1024)
.lpUserName = StringFromPointer(buffer(3), 1024)
.lpDocumentName = StringFromPointer(buffer(4), 1024)
.lpNotifyName = StringFromPointer(buffer(5), 1024)
.lpDataType = StringFromPointer(buffer(6), 1024)
.lpPrintProcessor = StringFromPointer(buffer(7), 1024)
.lpParameters = StringFromPointer(buffer(8), 1024)
.lpDriverName = StringFromPointer(buffer(9), 1024)
.lpDevMode = buffer(10) 'To do: Replace with ApiDevMode class when available
.lpStatus = StringFromPointer(buffer(11), 1024)
.lpSecurityDescriptor = buffer(12) 'To do: Replace...
.Status = buffer(13)
.Priority = buffer(14)
.Position = buffer(15)
.StartTime = buffer(16)
.UntilTime = buffer(17)
.TotalPages = buffer(18)
.JobSize = buffer(19)
'Submitted is here....
'4 long values, packed with 8 integers
With .Submitted
.wYear = LoWord(buffer(20))
.wMonth = HiWord(buffer(20))
.wDayOfWeek = LoWord(buffer(21))
.wDay = HiWord(buffer(21))
.wHour = LoWord(buffer(22))
.wMinute = HiWord(buffer(22))
.wSecond = LoWord(buffer(23))
.wMilliseconds = HiWord(buffer(23))
End With
.time = buffer(24)
.PagesPrinted = buffer(25)
End With
Case 3
With mJOB_INFO_3
.JobId = buffer(0)
.NextJobId = buffer(1)
.Reserved = buffer(2)
End With
End Select
End If
End If
End IfEnd SubPublic Sub ResumeJob()Dim lret As Longlret = SetJob(mhPrinter, mJobId, 0, 0, JOB_CONTROL_RESUME)
If Err.LastDllError Then
ReportError Err.LastDllError, CLSNAME & ":ResumeJob", GetLastSystemError
End IfEnd SubPrivate Sub SavePrintJobInfo(ByVal Level As Long)Dim lret As LongSelect Case Level
Case 1
'\\ Overwrite existing buffer data with that held locally in JOB_INFO_1
With mJOB_INFO_1
buffer(0) = .JobId '\\ This cannot change and is ignored
buffer(1) = LPCSTR(.lpPrinterName) '\\ This is ignored and cannot be changed
buffer(2) = LPCSTR(.lpMachinename) '\\ also ignored
buffer(3) = LPCSTR(.lpUserName)
buffer(4) = LPCSTR(.lpDocumentName)
buffer(5) = LPCSTR(.lpDataType)
buffer(6) = LPCSTR(.lpStatus)
buffer(7) = .Status
buffer(8) = .Priority
buffer(9) = .Position
buffer(10) = .TotalPages
buffer(11) = .PagesPrinted
With .Submitted '\\ Also ignored
buffer(12) = MakeLong(.wYear, .wMonth)
buffer(13) = MakeLong(.wDayOfWeek, .wDay)
buffer(14) = MakeLong(.wHour, .wMinute)
buffer(15) = MakeLong(.wSecond, .wMilliseconds)
End With
End WithCase 2
'\\ Overwrite existing buffer data with that held locally in JOB_INFO_2
With mJOB_INFO_2
buffer(0) = .JobId '\\ This cannot change and is ignored
buffer(1) = LPCSTR(.lpPrinterName) '\\ This is ignored and cannot be changed
buffer(2) = LPCSTR(.lpMachinename) '\\ also ignored
buffer(3) = LPCSTR(.lpUserName)
buffer(4) = LPCSTR(.lpDocumentName)
buffer(5) = LPCSTR(.lpNotifyName)
buffer(6) = LPCSTR(.lpDataType)
buffer(7) = LPCSTR(.lpPrintProcessor)
buffer(8) = LPCSTR(.lpParameters)
buffer(9) = LPCSTR(.lpDriverName)
buffer(10) = .lpDevMode
buffer(11) = LPCSTR(.lpStatus)
buffer(12) = .lpSecurityDescriptor
buffer(13) = .Status
buffer(14) = .Priority
buffer(15) = .Position
buffer(16) = .StartTime
buffer(17) = .UntilTime
buffer(18) = .TotalPages
buffer(19) = .JobSize
With .Submitted '\\ Also ignored
buffer(20) = MakeLong(.wYear, .wMonth)
buffer(21) = MakeLong(.wDayOfWeek, .wDay)
buffer(22) = MakeLong(.wHour, .wMinute)
buffer(23) = MakeLong(.wSecond, .wMilliseconds)
End With
buffer(24) = .time
buffer(25) = .PagesPrinted
End WithCase 3
'\\ Overwrite existing buffer data with that held locally in JOB_INFO_3
With mJOB_INFO_3
buffer(0) = .JobId
buffer(1) = .NextJobId
buffer(2) = .Reserved
End WithEnd Selectlret = SetJob(mhPrinter, mJobId, Level, buffer(0), 0)
If Err.LastDllError Then
ReportError Err.LastDllError, CLSNAME & ":SavePrintJobInfo", GetLastSystemError
Else
'\\ Free up "long pointer to string" strings...
Call ResetLPCSTRIndexes
End IfEnd Sub
'\\ --[Status]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get Status() As PrintJobStatusesIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
Status = mJOB_INFO_1.Status
End IfEnd Property
'\\ --[Submitted]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get Submitted() As APISystemTimeDim oSystemTime As APISystemTimeCall RefreshJobInfo(1)Set oSystemTime = New APISystemTime
oSystemTime.CreateFromPointer (VarPtr(mJOB_INFO_1.Submitted))Set Submitted = oSystemTimeEnd Property'\\ --[Status]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get TotalPages() As LongIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
TotalPages = mJOB_INFO_1.TotalPages
End IfEnd Property'\\ --[StartTime]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get StartTime() As LongCall RefreshJobInfo(2)
StartTime = mJOB_INFO_2.StartTimeEnd PropertyPublic Property Get UntilTime() As LongCall RefreshJobInfo(2)
UntilTime = mJOB_INFO_2.UntilTimeEnd Property
Public Property Let Username(ByVal newname As String)Call RefreshJobInfo(1)
mJOB_INFO_1.lpUserName = newname
mJOB_INFO_1.Position = JOB_POSITION_UNSPECIFIED
Call SavePrintJobInfo(1)End Property'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get Username() As String Call RefreshJobInfo(1)
Username = mJOB_INFO_1.lpUserNameEnd Property
'\\ --[Status]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get Status() As PrintJobStatusesIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
Status = mJOB_INFO_1.Status
End IfEnd Property
'\\ --[Submitted]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get Submitted() As APISystemTimeDim oSystemTime As APISystemTimeCall RefreshJobInfo(1)Set oSystemTime = New APISystemTime
oSystemTime.CreateFromPointer (VarPtr(mJOB_INFO_1.Submitted))Set Submitted = oSystemTimeEnd Property'\\ --[Status]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get TotalPages() As LongIf mhPrinter <> 0 And mJobId <> 0 Then
Call RefreshJobInfo(1)
TotalPages = mJOB_INFO_1.TotalPages
End IfEnd Property'\\ --[StartTime]-----------------------------------------------------------------------------------------------
'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get StartTime() As LongCall RefreshJobInfo(2)
StartTime = mJOB_INFO_2.StartTimeEnd PropertyPublic Property Get UntilTime() As LongCall RefreshJobInfo(2)
UntilTime = mJOB_INFO_2.UntilTimeEnd Property
Public Property Let Username(ByVal newname As String)Call RefreshJobInfo(1)
mJOB_INFO_1.lpUserName = newname
mJOB_INFO_1.Position = JOB_POSITION_UNSPECIFIED
Call SavePrintJobInfo(1)End Property'\\ ---------------------------------------------------------------------------------------------------------
Public Property Get Username() As String Call RefreshJobInfo(1)
Username = mJOB_INFO_1.lpUserNameEnd Property