检测打印机问题

yinyindog 2008-03-14 05:25:28
我做了个程序,其中需要检测打印机,不知道怎么编,具体地说就是要检测打印机是否连接计算机,更进一步检测打印机是否在运行或者是否缺纸之类状态,网上搜过一些方法,不过大多数只能检测计算机是否安装了打印驱动,不能检测出打印机真正连接到计算机,我觉得只能用api来实现这个功能,我对api不是太了解,只知道里面有个打印函数,不晓得怎么用,请高手帮帮忙想想有什么办法可以检测打印机的状态,先谢谢了~~
...全文
192 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzyong00 2008-03-21
  • 打赏
  • 举报
回复
用了WMI,还是比较方便的,不是VB自带的,是操作系统带的
yinyindog 2008-03-21
  • 打赏
  • 举报
回复
非常感激高手的回答,不过我还有几个问题想请教,这套程序是基于任何型号打印机的吗?这些代码没有引用到api,是vb里自带的与打印有关的函数?
cbm6666 2008-03-20
  • 打赏
  • 举报
回复
'添加 Command1 Timer1

Option Explicit
Dim aa$, bb$, starttm&, i%
Dim objWMIService, objProcess, colProcess, strComputer, strList, colInstalledPrinters, objPrinter

Private Sub Form_Load()
Timer1.Interval = 500
Timer1.Enabled = False
Command1.Caption = "打 印"
End Sub

Private Sub Command1_Click()
If Command1.Caption = "打 印" Then
If Printers.Count > 0 And Printer.DriverName <> "" Then
Command1.Caption = "取 消"
Open "c:\tt.txt" For Input As #1
starttm = Timer
Timer1.Enabled = True
Me.Caption = "打印机检测中"
While Not EOF(1)
Line Input #1, aa
Printer.Print aa
Wend
Close #1
Printer.EndDoc
Else
MsgBox "您未设置打印机或未安装驱动程序"
End If
Else
Command1.Caption = "打 印"
Timer1.Enabled = False
Printer.KillDoc
End If
End Sub

Private Sub Timer1_Timer()
strComputer = "."
Set objWMIService = GetObject("winmgmts:" + "{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("SELECT * FROM Win32_Printer")
bb = ""
For Each objPrinter In colInstalledPrinters
bb = bb & objPrinter.PrinterStatus
If InStr(bb, "4") > 0 Then Timer1.Enabled = False: Command1_Click: Exit Sub
Next
If Timer - starttm > 15 Then
Timer1.Enabled = False
MsgBox "打印机未联线或缺纸或予热中"
Command1_Click
End If
End Sub

YFY 2008-03-19
  • 打赏
  • 举报
回复
另外一种,用VC写个动态库接口,VB调用:

使用专用打印机 ,没有驱动程序,我用直接往lpt1写的方法打印,如果打印机联接正常, 打印的数据会正常输出,但是当打印机未联机或打印机没开,程序打印时就会死机,只能按Ctrl+Alt+Del强行终止 。有什么办法可以判断打印机状态,或是如何设置一个timeout时间来终止 打印 ?

方法如下:

bool LptPrint(char prtdata[],int prtlen,int timeout)
{
HANDLE h;
DWORD n;
COMMTIMEOUTS t;
bool result;
h = CreateFile("lpt1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (h == INVALID_HANDLE_VALUE){
AfxMessageBox("Can not open lpt1");
return false;
}
t.ReadIntervalTimeout = 0;
t.ReadTotalTimeoutMultiplier = 0;
t.ReadTotalTimeoutConstant = 0;
t.WriteTotalTimeoutMultiplier = timeout * 1000 / prtlen;
t.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts(h,&t)){
AfxMessageBox("SetCommTimeout error");
return false;
}
result = true;
if (!WriteFile(h,prtdata,prtlen,&n,NULL)){
AfxMessageBox("Print error");
result = false;
}
CloseHandle(h);
return result;
}
YFY 2008-03-19
  • 打赏
  • 举报
回复
转载:

打印检查的问题 华软论坛 2006-01-15 11:41:02 在 VB / 数据库(包含打印,安装,报表) 提问
第一次检测正常,但第二次检测时就出错了

就是说打印第一次就可以,当第二次打印时就出问题了,以前在别的打印不会现在换成LQ-630+的打印就不可以了,请问这是什么原因呀?

相关代码:
Option Explicit
Declare Function MapPhysToLin Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysSize As Long, ByRef PhysMemHandle) As Long
Declare Function UnmapPhysicalMemory Lib "WinIo.dll" (ByVal PhysMemHandle, ByVal LinAddr) As Boolean
Declare Function GetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByRef PhysVal As Long) As Boolean
Declare Function SetPhysLong Lib "WinIo.dll" (ByVal PhysAddr As Long, ByVal PhysVal As Long) As Boolean
Declare Function GetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByRef PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function SetPortVal Lib "WinIo.dll" (ByVal PortAddr As Integer, ByVal PortVal As Long, ByVal bSize As Byte) As Boolean
Declare Function InitializeWinIo Lib "WinIo.dll" () As Boolean
Declare Function ShutdownWinIo Lib "WinIo.dll" () As Boolean
Declare Function InstallWinIoDriver Lib "WinIo.dll" (ByVal DriverPath As String, ByVal Mode As Integer) As Boolean
Declare Function RemoveWinIoDriver Lib "WinIo.dll" () As Boolean
Public IOStat As Boolean
'************************************************************
'*函数名称: GetPrnStat *
'*功能: 根据打印机的内存地址,检测打印机的目前工作状态 *
'*参数: *
'* lptport: 要检测的打印机的端口号,如LPT1: *
'*返回值: 打印机的工作状态值。 *
'* 0:正常 1:缺纸 2:无联系 3:异常(其他错误) *
'*调用:本模块中的API函数InitializeWinIo和GetPortVal *
'*备注:检测的内存地址,是在打印端口所在的基地址上加1; *
'*作者: 谷霖 *
'* LPT1口的基地址为&H378;LPT2口的基地址为&H278 *
'************************************************************
Public Function GetPrnStat(ByVal LptPort As String) As Long
Dim PrnAddr As Long
On Error Resume Next
If IOStat = False Then IOStat = InitializeWinIo()
If IOStat Then
If UCase(LptPort) = "LPT1:" Then
PrnAddr = &H379
ElseIf UCase(LptPort) = "LPT2:" Then
PrnAddr = &H279
End If
GetPortVal PrnAddr, GetPrnStat, 1
Else
GetPrnStat = &HFF
End If
GetPrnStat = GetPrnStat And &HF8
Select Case GetPrnStat
Case &H68, &H58, &H70
GetPrnStat = 1 '缺纸
Case &H78
GetPrnStat = 2 '无联系
Case &HD8
GetPrnStat = 0 '正常
Case Else
GetPrnStat = 3 '异常
End Select
End Function

'********************************************************************************************
'* 函数功能:检查打印机的状态主函数   *
'* 输入参数: *
'* PrintName 要检测的打印机名称 *
'* 输出参数: *
'* checkprinterr *
'* 检查结果(0:正常 1:打印机缺纸 2:打印机无联系 3:打印机异常 *
'* 4:没有安装打印机 5:打印机名称错误) *
'********************************************************************************************
Public Function CheckPrintErr(ByVal PrintName As String) As Long
'CheckPrintErr参数说明
'0:没有错误
'1:打印机无联系
'2:打印机缺纸
'3:没有安装打印机
Dim printjieguo As Long
Dim i As Long, k As Long
On Error GoTo ErrCheckPrint

If Printers.Count = 0 Then
CheckPrintErr = 4 '没有安装打印机
Exit Function
End If
'检测发票打印机是否可以联系
For i = 0 To Printers.Count - 1
If (Printers(i).DeviceName = PrintName) Then
k = k + 1
Exit For
End If
Next
If k = 0 Then '打印机名称错误
CheckPrintErr = 5
Exit Function
End If
Set Printer = Printers(i)

CheckPrintErr = GetPrnStat(Printer.Port)
Exit Function
ErrCheckPrint:
CheckPrintErr = 3
Exit Function
End Function


CathySun118 2008-03-19
  • 打赏
  • 举报
回复

on error goto msg
err.clear
printer.enddoc
msg:
msgbox "没有正常连接打印机 "
相关推荐
发帖
API

1483

社区成员

VB API
社区管理员
  • API
加入社区
帖子事件
创建了帖子
2008-03-14 05:25
社区公告
暂无公告