请问,怎样使用GetPrinter()函数获得打印机的状态? 谢谢!!

scyn 2004-09-28 03:03:31
我用的OS是Windows 2000专业版

我想使用下面的方法获得打印机状态,可是GetPrinter()函数不会用

Private Enum Printer_Status
PRINTER_STATUS_READY = &H0
PRINTER_STATUS_PAUSED = &H1
PRINTER_STATUS_ERROR = &H2
PRINTER_STATUS_PENDING_DELETION = &H4
PRINTER_STATUS_PAPER_JAM = &H8
PRINTER_STATUS_PAPER_OUT = &H10
PRINTER_STATUS_MANUAL_FEED = &H20
PRINTER_STATUS_PAPER_PROBLEM = &H40
PRINTER_STATUS_OFFLINE = &H80
PRINTER_STATUS_IO_ACTIVE = &H100
PRINTER_STATUS_BUSY = &H200
PRINTER_STATUS_PRINTING = &H400
PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
PRINTER_STATUS_NOT_AVAILABLE = &H1000
PRINTER_STATUS_WAITING = &H2000
PRINTER_STATUS_PROCESSING = &H4000
PRINTER_STATUS_INITIALIZING = &H8000
PRINTER_STATUS_WARMING_UP = &H10000
PRINTER_STATUS_TONER_LOW = &H20000
PRINTER_STATUS_NO_TONER = &H40000
PRINTER_STATUS_PAGE_PUNT = &H80000
PRINTER_STATUS_USER_INTERVENTION = &H100000
PRINTER_STATUS_OUT_OF_MEMORY = &H200000
PRINTER_STATUS_DOOR_OPEN = &H400000
PRINTER_STATUS_SERVER_UNKNOWN = &H800000
PRINTER_STATUS_POWER_SAVE = &H1000000
End Enum

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 GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, buffer As Long, ByVal pbSize As Long, pbSizeNeeded As Long) As Long

Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long

Private Sub Command1_Click()
MsgBox IsPrinterReady(Printer.DeviceName)
End Sub

Private Function IsPrinterReady(ByVal strDeviceName As String) As Boolean
Dim hPrinter As Long, lret As Long, SizeNeeded As Long
Dim buffer(1000) as long

lret = OpenPrinter(Printer.DeviceName, hPrinter, 0&)

lret = GetPrinter(hPrinter, 7, buffer(0), 1000, SizeNeeded) '返回值总=0(调用失败)

ClosePrinter hPrinter

IsPrinterReady = (buffer(0) = Printer_Status.PRINTER_STATUS_READY)

End Function

返回值总=0(调用失败),而且buffer(0)也总 = 0 没有获得打印机状态,是参数不正确吗??
第2个参数从1—7我都试过了

小弟跪谢~~~~! 急盼~~~~~






...全文
828 点赞 收藏 20
写回复
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunql 2004-10-13
scyn() 你试过即使没有连接打印机,打印机关机,或者打印机缺纸等,他的状态好像都是ready!
你试过除了能够获取cJobs的状态还可以获取什么状态呢?
回复
scyn 2004-10-13
Sunql(景成),我用的情况和你说的是一样的,text1.text的内容始终是显示ready,text2的内容显示的是打印机的名称、驱动和端口等信息,应该没问题吧,因为我就是想得到打印机的工作状态,如:打印文档的数量cJobs,所以这部分对我来说是好用的:)
回复
Sunql 2004-10-13
这个程序我以前都试过,但是我觉得无论怎样测试Text1.Text和PrinterStatus的内容始终没有变过,而JobStatus是可以的。scyn() 你遇到这种情况吗?
回复
scyn 2004-10-12
接着上面的代码:

3、Add three CommandButton controls.
4、Add three TextBox controls to Form1, and then configure the TextBox controls as follows:
a、Set the MultiLine property of each TextBox control to True.
b、Size each TextBox to approximately five inches wide and three inches long.
c、Set the ScrollBars property of each TextBox control to 2 - Vertical.
5、Add a Timer control to Form1.
6、Add the following code to the Form's module:

Option Explicit

Private Sub Command1_Click()
'Enable the timer to begin printer status checks.
Timer1.Enabled = True

'Enable and disable start and stop buttons.
Command1.Enabled = False
Command2.Enabled = True
Command3.Enabled = True
End Sub

Private Sub Command2_Click()
'Disable timer to stop further printer checks.
Timer1.Enabled = False

'Enable and disable start and stop buttons.
Command1.Enabled = True
Command2.Enabled = False
Command3.Enabled = True
End Sub

Private Sub Command3_Click()
'Clear the status info.
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub

Private Sub Form_Load()
'Initialize captions for control buttons.
Command1.Caption = "Start"
Command2.Caption = "Stop"
Command3.Caption = "Clear"

'Clear the status info.
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""

Command1.Enabled = True
'Disable stop and clear buttons.
Command2.Enabled = False
Command3.Enabled = False

'Set interval for printer status checking to 1/2 second.
Timer1.Enabled = False
Timer1.Interval = 500
End Sub

Private Sub Timer1_Timer()
Dim PrinterStatus As String
Dim JobStatus As String
Dim ErrorInfo As String

'Clear the status info for new info/status.
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""

'Call sub to perform check.
Text1.Text = CheckPrinter(PrinterStatus, JobStatus)
Text2.Text = PrinterStatus
Text3.Text = JobStatus
End Sub
回复
scyn 2004-10-12
我在微软的网站上,找到了关于取得打印机状态的VB代码,其中就使用了GetPrinter()等函数,我在VB6 + Windows 2000下试过了,好用,有兴趣的朋友可以参考一下。

只要按照提示(Step-by-Step Example),将代码拷贝到程序里就可以了。

接着上面的代码:

Public Function GetString(ByVal PtrStr As Long) As String
Dim StrBuff As String * 256

'Check for zero address
If PtrStr = 0 Then
GetString = " "
Exit Function
End If

'Copy data from PtrStr to buffer.
CopyMemory ByVal StrBuff, ByVal PtrStr, 256

'Strip any trailing nulls from string.
GetString = StripNulls(StrBuff)
End Function

Public Function StripNulls(OriginalStr As String) As String
'Strip any trailing nulls from input string.
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If

'Return modified string.
StripNulls = OriginalStr
End Function

Public Function PtrCtoVbString(Add As Long) As String
Dim sTemp As String * 512
Dim x As Long

x = lstrcpy(sTemp, Add)
If (InStr(1, sTemp, Chr(0)) = 0) Then
PtrCtoVbString = ""
Else
PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
End If
End Function

Public Function CheckPrinterStatus(PI2Status As Long) As String
Dim tempStr As String

If PI2Status = 0 Then ' Return "Ready"
CheckPrinterStatus = "Printer Status = Ready" & vbCrLf
Else
tempStr = "" ' Clear
If (PI2Status And PRINTER_STATUS_BUSY) Then
tempStr = tempStr & "Busy "
End If

If (PI2Status And PRINTER_STATUS_DOOR_OPEN) Then
tempStr = tempStr & "Printer Door Open "
End If

If (PI2Status And PRINTER_STATUS_ERROR) Then
tempStr = tempStr & "Printer Error "
End If

If (PI2Status And PRINTER_STATUS_INITIALIZING) Then
tempStr = tempStr & "Initializing "
End If

If (PI2Status And PRINTER_STATUS_IO_ACTIVE) Then
tempStr = tempStr & "I/O Active "
End If

If (PI2Status And PRINTER_STATUS_MANUAL_FEED) Then
tempStr = tempStr & "Manual Feed "
End If

If (PI2Status And PRINTER_STATUS_NO_TONER) Then
tempStr = tempStr & "No Toner "
End If

If (PI2Status And PRINTER_STATUS_NOT_AVAILABLE) Then
tempStr = tempStr & "Not Available "
End If

If (PI2Status And PRINTER_STATUS_OFFLINE) Then
tempStr = tempStr & "Off Line "
End If

If (PI2Status And PRINTER_STATUS_OUT_OF_MEMORY) Then
tempStr = tempStr & "Out of Memory "
End If

If (PI2Status And PRINTER_STATUS_OUTPUT_BIN_FULL) Then
tempStr = tempStr & "Output Bin Full "
End If

If (PI2Status And PRINTER_STATUS_PAGE_PUNT) Then
tempStr = tempStr & "Page Punt "
End If

If (PI2Status And PRINTER_STATUS_PAPER_JAM) Then
tempStr = tempStr & "Paper Jam "
End If

If (PI2Status And PRINTER_STATUS_PAPER_OUT) Then
tempStr = tempStr & "Paper Out "
End If

If (PI2Status And PRINTER_STATUS_OUTPUT_BIN_FULL) Then
tempStr = tempStr & "Output Bin Full "
End If

If (PI2Status And PRINTER_STATUS_PAPER_PROBLEM) Then
tempStr = tempStr & "Page Problem "
End If

If (PI2Status And PRINTER_STATUS_PAUSED) Then
tempStr = tempStr & "Paused "
End If

If (PI2Status And PRINTER_STATUS_PENDING_DELETION) Then
tempStr = tempStr & "Pending Deletion "
End If

If (PI2Status And PRINTER_STATUS_PRINTING) Then
tempStr = tempStr & "Printing "
End If

If (PI2Status And PRINTER_STATUS_PROCESSING) Then
tempStr = tempStr & "Processing "
End If

If (PI2Status And PRINTER_STATUS_TONER_LOW) Then
tempStr = tempStr & "Toner Low "
End If

If (PI2Status And PRINTER_STATUS_USER_INTERVENTION) Then
tempStr = tempStr & "User Intervention "
End If

If (PI2Status And PRINTER_STATUS_WAITING) Then
tempStr = tempStr & "Waiting "
End If

If (PI2Status And PRINTER_STATUS_WARMING_UP) Then
tempStr = tempStr & "Warming Up "
End If

'Did you find a known status?
If Len(tempStr) = 0 Then
tempStr = "Unknown Status of " & PI2Status
End If

'Return the Status
CheckPrinterStatus = "Printer Status = " & tempStr & vbCrLf
End If
End Function

回复
scyn 2004-10-12
1、Create a new Standard EXE project in Visual Basic. By default, Form1 is created.
2、On the Project menu, click Add Module, and then add the following code:

Option Explicit

Public Declare Function lstrcpy Lib "kernel32" _
Alias "lstrcpyA" _
(ByVal lpString1 As String, _
ByVal lpString2 As String) _
As Long

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 GetPrinter Lib "winspool.drv" Alias "GetPrinterA" _
(ByVal hPrinter As Long, _
ByVal Level As Long, _
pPrinter As Byte, _
ByVal cbBuf As Long, _
pcbNeeded As Long) _
As Long

Public Declare Function ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) _
As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)

Public Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" _
(ByVal hPrinter As Long, _
ByVal FirstJob As Long, _
ByVal NoJobs As Long, _
ByVal Level As Long, _
pJob As Byte, _
ByVal cdBuf As Long, _
pcbNeeded As Long, _
pcReturned As Long) _
As Long

' constants for PRINTER_DEFAULTS structure
Public Const PRINTER_ACCESS_USE = &H8
Public Const PRINTER_ACCESS_ADMINISTER = &H4

' constants for DEVMODE structure
Public Const CCHDEVICENAME = 32
Public Const CCHFORMNAME = 32

Public Type PRINTER_DEFAULTS
pDatatype As String
pDevMode As Long
DesiredAccess As Long
End Type

Public Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmLogPixels As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type

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 Type

Type JOB_INFO_2
JobId As Long
pPrinterName As Long
pMachineName As Long
pUserName As Long
pDocument As Long
pNotifyName As Long
pDatatype As Long
pPrintProcessor As Long
pParameters As Long
pDriverName As Long
pDevMode As Long
pStatus As Long
pSecurityDescriptor As Long
Status As Long
Priority As Long
Position As Long
StartTime As Long
UntilTime As Long
TotalPages As Long
Size As Long
Submitted As SYSTEMTIME
time As Long
PagesPrinted As Long
End Type

Type PRINTER_INFO_2
pServerName As Long
pPrinterName As Long
pShareName As Long
pPortName As Long
pDriverName As Long
pComment As Long
pLocation As Long
pDevMode As Long
pSepFile As Long
pPrintProcessor As Long
pDatatype As Long
pParameters As Long
pSecurityDescriptor As Long
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type

Public Const ERROR_INSUFFICIENT_BUFFER = 122
Public Const PRINTER_STATUS_BUSY = &H200
Public Const PRINTER_STATUS_DOOR_OPEN = &H400000
Public Const PRINTER_STATUS_ERROR = &H2
Public Const PRINTER_STATUS_INITIALIZING = &H8000
Public Const PRINTER_STATUS_IO_ACTIVE = &H100
Public Const PRINTER_STATUS_MANUAL_FEED = &H20
Public Const PRINTER_STATUS_NO_TONER = &H40000
Public Const PRINTER_STATUS_NOT_AVAILABLE = &H1000
Public Const PRINTER_STATUS_OFFLINE = &H80
Public Const PRINTER_STATUS_OUT_OF_MEMORY = &H200000
Public Const PRINTER_STATUS_OUTPUT_BIN_FULL = &H800
Public Const PRINTER_STATUS_PAGE_PUNT = &H80000
Public Const PRINTER_STATUS_PAPER_JAM = &H8
Public Const PRINTER_STATUS_PAPER_OUT = &H10
Public Const PRINTER_STATUS_PAPER_PROBLEM = &H40
Public Const PRINTER_STATUS_PAUSED = &H1
Public Const PRINTER_STATUS_PENDING_DELETION = &H4
Public Const PRINTER_STATUS_PRINTING = &H400
Public Const PRINTER_STATUS_PROCESSING = &H4000
Public Const PRINTER_STATUS_TONER_LOW = &H20000
Public Const PRINTER_STATUS_USER_INTERVENTION = &H100000
Public Const PRINTER_STATUS_WAITING = &H2000
Public Const PRINTER_STATUS_WARMING_UP = &H10000
Public Const JOB_STATUS_PAUSED = &H1
Public Const JOB_STATUS_ERROR = &H2
Public Const JOB_STATUS_DELETING = &H4
Public Const JOB_STATUS_SPOOLING = &H8
Public Const JOB_STATUS_PRINTING = &H10
Public Const JOB_STATUS_OFFLINE = &H20
Public Const JOB_STATUS_PAPEROUT = &H40
Public Const JOB_STATUS_PRINTED = &H80
Public Const JOB_STATUS_DELETED = &H100
Public Const JOB_STATUS_BLOCKED_DEVQ = &H200
Public Const JOB_STATUS_USER_INTERVENTION = &H400
Public Const JOB_STATUS_RESTART = &H800
回复
Sunql 2004-10-08
UP
回复
Sunql 2004-10-07
楼上的大哥,怎么我试过无论有没有连打印机,有没有打印作业,打印机是否开机。
它总是显示122
0
是否我使用不当呢?
我就是讲你的代码全部粘貼到form中然后调用Main()
回复
lxcc 2004-10-03
' *************************************************************************
' Copyright ?001 Karl E. Peterson
' All Rights Reserved, http://www.mvps.org/vb
' *************************************************************************
' You are free to use this code within your own applications, but you
' are expressly forbidden from selling or otherwise distributing this
' source code, non-compiled, without prior written consent.
' *************************************************************************
Option Explicit

' Win32 API declares
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrn As Long, pDefault As Any) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrn As Long) As Long
Private Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long

' The data area passed to a system call is too small.
Private Const ERROR_INSUFFICIENT_BUFFER = 122&

' Values used to define DEVMODE structure
Private Const CCHDEVICENAME As Long = 32
Private Const CCHFORMNAME As Long = 32

Private Type ACL
AclRevision As Byte
Sbz1 As Byte
AclSize As Integer
AceCount As Integer
Sbz2 As Integer
End Type

Private Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As ACL
Dacl As ACL
End Type

Private Type DevMode
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer ' not exposed
dmDriverExtra As Integer ' not exposed
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmLogPixels As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmNup As Long ' union with dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type

Private Type PRINTER_INFO_2
pServerName As String
pPrinterName As String
pShareName As String
pPortName As String
pDriverName As String
pComment As String
pLocation As String
pDevMode As Long 'DEVMODE
pSepFile As String
pPrintProcessor As String
pDatatype As String
pParameters As String
pSecurityDescriptor As Long 'SECURITY_DESCRIPTOR
Attributes As Long
Priority As Long
DefaultPriority As Long
StartTime As Long
UntilTime As Long
Status As Long
cJobs As Long
AveragePPM As Long
End Type

Public Sub Main()
Dim prn As Printer
For Each prn In Printers
Debug.Print JobCount(prn.DeviceName)
Next prn
End Sub

Public Function JobCount(ByVal DevName As String) As Long
Dim hPrn As Long
Dim BytesNeeded As Long
Dim BytesUsed As Long
Dim pi2 As PRINTER_INFO_2
Const StrSize As Long = 256

' init string elements
pi2.pServerName = Space$(StrSize)
pi2.pPrinterName = Space$(StrSize)
pi2.pShareName = Space$(StrSize)
pi2.pPortName = Space$(StrSize)
pi2.pDriverName = Space$(StrSize)
pi2.pComment = Space$(StrSize)
pi2.pLocation = Space$(StrSize)
pi2.pSepFile = Space$(StrSize)
pi2.pPrintProcessor = Space$(StrSize)
pi2.pDatatype = Space$(StrSize)
pi2.pParameters = Space$(StrSize)

Call OpenPrinter(DevName, hPrn, ByVal 0&)
If hPrn Then
If GetPrinter(hPrn, 2, pi2, LenB(pi2), BytesUsed) Then
JobCount = pi2.cJobs
Else
Debug.Print Err.LastDllError
End If
Call ClosePrinter(hPrn)
End If
End Function
回复
lxcc 2004-10-03
函数名 GetPrinter
别名 GetPrinterA
分类 文件处理函数
VB声明 Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long
参数表 参数 类型及说明hPrinter ------- Long,一个已打开的打印机的句柄(用OpenPrinter获得)

Level ---------- Long,1,2,3(仅适用于NT),4(仅适用于NT),或者5(仅适用于Windows
95 和 NT 4.0)

pPrinter ------- Any,包含PRINTER_INFO_x结构的缓冲区。x代表级别

cbBuf ---------- Long,pPrinterEnum缓冲区中的字符数量

pcbNeeded ------ Long,指向一个Long型变量的指针,该变量用于保存请求的缓冲区长度,或者实际读入的字节数量



返回值 Long,非零表示成功,零表示失败。会设置GetLastError
Win95支持 Yes
WinNT支持 Yes
所属库文件 winspool.drv
说明 取得与指定打印机有关的信息
注解 只有在发出调用的应用程序有足够的权限时,PRINTER_INFO_x结构中的一些字段才能够被读取。这种权限由系统当前的安全设置决定
回复
scyn 2004-10-03
顶!
回复
scyn 2004-09-30
请问有高手使用过getprinter函数吗?

谢谢!
回复
leolan 2004-09-29
幫你頂!
回复
scyn 2004-09-29
我就是想判断打印机是否正在打印,当前打印是否完毕。

我以前是判断打印队列中是否有打印任务,对普通的打印机此方法好用,可是实际情况是:一台带内存的网络打印机(Epson AL-C4000),打印队列中根本没有打印任务,而是通过网络打印机直接打印,所以我想通过getprinter直接判断打印机的状态,这样就可以在程序中加以控制。

谢谢!
回复
scyn 2004-09-29
请问,我的代码有什么问题吗(GetPrinter()部分)?小弟我水平有限,微软的Knowledge Base的文章我实在是看不懂?有没有简单的方法?

谢谢!谢谢!
回复
kongmj 2004-09-29
目前在微软的网站上没有现成的关于getprinter API调用的VB代码,但有一篇VC的示例代码,您可以参考一下:

   HOWTO: Get the Status of a Printer and a Print Job
   (http://support.microsoft.com/support/kb/articles/Q160/1/29.asp)
回复
jam021 2004-09-29
关注,帮你顶!
回复
RUKYO 2004-09-28
来自:http://www.china-askpro.com/msg9/qa81.shtml

打印资料时, 如何得到打印机的状态

编号:QA000881
建立日期: 1999年4月28日 最后修改日期:2002年4月2日
所属类别:

Visual Basic - 打印与报表

田雨:
vb v5.0
win95 & nt v4.0
打印资料时, 如何得到打印机的状态? 例如, 打印前检测打印机是否联机, 打印后检测是否打印过程是否正常?

回答:

你必须使用Windows API,具体步骤参考:微软的Knowledge Base的文章:“Q160129 OWTO: Get the Status of a Printer and a Print Job”。下面是部分例子:
BOOL GetJobs(HANDLE hPrinter, /* Handle to the printer. */

JOB_INFO_2 **ppJobInfo, /* Pointer to be filled. */
int *pcJobs, /* Count of jobs filled. */
DWORD *pStatus) /* Print Queue status. */

{

DWORD cByteNeeded,
nReturned,
cByteUsed;
JOB_INFO_2 *pJobStorage = NULL;
PRINTER_INFO_2 *pPrinterInfo = NULL;

/* Get the buffer size needed. */
if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return FALSE;
}

pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
if (!(pPrinterInfo))
/* Failure to allocate memory. */
return FALSE;

/* Get the printer information. */
if (!GetPrinter(hPrinter,
2,
(LPSTR)pPrinterInfo,
cByteNeeded,
&cByteUsed))
{
/* Failure to access the printer. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}

/* Get job storage space. */
if (!EnumJobs(hPrinter,
0,
pPrinterInfo->cJobs,
2,
NULL,
0,
(LPDWORD)&cByteNeeded,
(LPDWORD)&nReturned))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}
}

pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);
if (!pJobStorage)
{
/* Failure to allocate Job storage space. */
free(pPrinterInfo);
pPrinterInfo = NULL;
return FALSE;
}

ZeroMemory(pJobStorage, cByteNeeded);

/* Get the list of jobs. */
if (!EnumJobs(hPrinter,
0,
pPrinterInfo->cJobs,
2,
(LPBYTE)pJobStorage,
cByteNeeded,
(LPDWORD)&cByteUsed,
(LPDWORD)&nReturned))
{
free(pPrinterInfo);
free(pJobStorage);
pJobStorage = NULL;
pPrinterInfo = NULL;
return FALSE;
}

/*
* Return the information.
*/
*pcJobs = nReturned;
*pStatus = pPrinterInfo->Status;
*ppJobInfo = pJobStorage;
free(pPrinterInfo);

return TRUE;

}

BOOL IsPrinterError(HANDLE hPrinter)
{

JOB_INFO_2 *pJobs;
int cJobs,
i;
DWORD dwPrinterStatus;

/*
* Get the state information for the Printer Queue and
* the jobs in the Printer Queue.
*/
if (!GetJobs(hPrinter, &pJobs, &cJobs, &dwPrinterStatus))
return FALSE;

/*
* If the Printer reports an error, believe it.
*/
if (dwPrinterStatus &
(PRINTER_STATUS_ERROR |
PRINTER_STATUS_PAPER_JAM |
PRINTER_STATUS_PAPER_OUT |
PRINTER_STATUS_PAPER_PROBLEM |
PRINTER_STATUS_OUTPUT_BIN_FULL |
PRINTER_STATUS_NOT_AVAILABLE |
PRINTER_STATUS_NO_TONER |
PRINTER_STATUS_OUT_OF_MEMORY |
PRINTER_STATUS_OFFLINE |
PRINTER_STATUS_DOOR_OPEN))
{
return TRUE;
}

/*
* Find the Job in the Queue that is printing.
*/
for (i=0; i < cJobs; i++)
{
if (pJobs[i].Status & JOB_STATUS_PRINTING)
{
/*
* If the job is in an error state,
* report an error for the printer.
* Code could be inserted here to
* attempt an interpretation of the
* pStatus member as well.
*/
if (pJobs[i].Status &
(JOB_STATUS_ERROR |
JOB_STATUS_OFFLINE |
JOB_STATUS_PAPEROUT |
JOB_STATUS_BLOCKED_DEVQ))
{
return TRUE;
}
}
}

/*
* No error condition.
*/
return FALSE;

}

回复
RUKYO 2004-09-28
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q160/1/29.asp&NoWebContent=1

回复
ryuginka 2004-09-28
uop
回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告