EnumJobs返回作业个数老是为0??
各位高手,在VBA中使用EnumJobs监控网络打印机,结果发现返回作业的个数老为0,但是这个函数本身返回值为1,也就是意味着连接上打印机了,代码如下:
Private Sub CommandButton1_Click()
Dim sPrintName1 As String
Dim hPrinter As Long
Dim pDefault As PRINTER_DEFAULTS
Dim lngJobsFirstJob As Long, lngJobsEnumJob As Long, lngJobsLevel As Long
Dim byteJobsBuffer() As Byte
Dim lngJobsNeeded As Long
Dim lngJobsReturned As Long
Dim lngResult As Long
Dim udtJobInfo1() As JOB_INFO_1
Dim byteBuffer(64) As Byte
On Error Resume Next
sPrintName1 = "FX DocuCentre-IV C2265 PCL 6 (B8#2F)"
lngResult = OpenPrinter(sPrintName1, hPrinter, pDefault)
lngJobsFirstJob = 0 ' zero-based position within the print queue of the first print job to enumerate
lngJobsEnumJob = 99 ' total number of print jobs to enumerate
lngJobsLevel = 1 ' Specifies whether the function should use JOB_INFO_1
' or JOB_INFO_2 structures to store data for the enumerated jobs
lngResult = EnumJobs(hPrinter, lngJobsFirstJob, lngJobsEnumJob, lngJobsLevel, ByVal 0&, 0, lngJobsNeeded, lngJobsReturned)
If lngJobsNeeded > 0 Then
MsgBox lngJobsReturned
ReDim byteJobsBuffer(lngJobsNeeded - 1)
ReDim udtJobInfo1(lngJobsNeeded - 1)
lngResult = EnumJobs(hPrinter, lngJobsFirstJob, lngJobsEnumJob, _
lngJobsLevel, byteJobsBuffer(0), lngJobsNeeded, _
lngJobsNeeded, lngJobsReturned)
If lngJobsReturned > 0 Then
MoveMemory udtJobInfo1(0), byteJobsBuffer(0), Len(udtJobInfo1(0)) * lngJobsReturned
For lngjobscount = 0 To lngJobsReturned - 1
With udtJobInfo1(lngjobscount)
' Get the document name
lngResult = lstrcpy(byteBuffer(0), ByVal .pDocument)
strDocument = StrConv(byteBuffer(), vbUnicode)
' Document name has been returned as null terminated-string
strDocument = Left$(strDocument, InStr(strDocument, vbNullChar) - 1)
Sheet1.Range("D" & lngjobscount + 2) = strDocument
' Get the document's owner name
lngResult = lstrcpy(byteBuffer(0), ByVal .pUserName)
strOwnerName = StrConv(byteBuffer(), vbUnicode)
' Document's owner name has been returned as null-terminated string
strOwnerName = Left$(strOwnerName, InStr(strOwnerName, vbNullChar) - 1)
Sheet1.Range("C" & lngjobscount + 2) = strOwnerName
End With
Next lngjobscount
Else
' number of jobs returned = 0 (no jobs)
lngjobscount = 0
End If
End If
lngResult = ClosePrinter(hPrinter)
End Sub
补充:VB , 基础类