La Web del Programador: Comunidad de Programadores
 
    Pregunta:  18471 - DETECTAR SI LA IMPRESORA ESTA EN LINEA
Autor:  Gabriel Martinez Bogado
Hola como estan , mi pregunta es la siguiente:
Como puedo hacer para mandar una impresion ya sea en crystal reports o por un printer , y que me detecte si la impresora esta en linea , o sea si esta prendido la impresora
Por favor si alguien puede ayudarme pasandome el codigo o guiandome de alguna forma
Desde ya muy agradecido

  Respuesta:  Luis Gonzalez Ruiz
Hola Gabriel.
Encontré una solución a tu problema.

Crea un proyecto nuevo
A nivel de modulo copía lo siguiente.
Public Const GENERIC_WRITE = &H40000000
Public Const GENERIC_READ = &H80000000
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const CREATE_ALWAYS = 2
Public Const OPEN_ALWAYS = 4
Public Const INVALID_HANDLE_VALUE = -1

Public Type COMSTAT
Filler1 As Long
Filler2 As Long
Filler3 As Long
Filler4 As Long
Filler5 As Long
Filler6 As Long
Filler7 As Long
Filler8 As Long
Filler9 As Long
Filler10 As Long
End Type

Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) _
As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Declare Function ClearCommError Lib "kernel32" (ByVal hFile As Long, lpErrors As Long, _
lpStat As COMSTAT) As Long

Public Const CE_BREAK = &H10 ' break condition
Public Const CE_PTO = &H200 ' printer timeout
Public Const CE_IOE = &H400 ' printer I/O error
Public Const CE_DNS = &H800 ' device not selected
Public Const CE_OOP = &H1000 ' out of paper

Y en un boton de comando el siguiente codigo y "Solución"
Private Sub btfunciona_Click()
Dim mHandle As Long
Dim lpErrors As Long
Dim x As COMSTAT

mHandle = CreateFile("lpt1", GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)

If mHandle < 0 Then
MsgBox "error de apertura del puerto " & mHandle
Else

If lpErrors And CE_BREAK Then
MsgBox "Error genérico"
End If

If lpErrors And CE_PTO Then
MsgBox "Timeout de impresora"
End If

If lpErrors And CE_IOE Then
MsgBox "Error de entrada/salida"
End If

If lpErrors And CE_DNS Then
MsgBox "Dispositivo no seleccionado" 'impresora apagada o algo por el estilo
End If

If lpErrors And CE_OOP Then
MsgBox "Sin papel"
End If

CloseHandle mHandle
End If
End Sub

Espero que te sirva y si es así por favor Comunicamelo.

  Respuesta:  Luis Gonzalez
Hola Gabriel.
Soy el mismo que envió en anterior pero se fue con un pequeño desperfecto. Lo correcto es la Sgte.

A nivel de modulo.
Public Const GENERIC_WRITE = &H40000000
Public Const GENERIC_READ = &H80000000
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const CREATE_ALWAYS = 2
Public Const OPEN_ALWAYS = 4
Public Const INVALID_HANDLE_VALUE = -1

Public Type COMSTAT
Filler1 As Long
Filler2 As Long
Filler3 As Long
Filler4 As Long
Filler5 As Long
Filler6 As Long
Filler7 As Long
Filler8 As Long
Filler9 As Long
Filler10 As Long
End Type

Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) _
As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Declare Function ClearCommError Lib "kernel32" (ByVal hFile As Long, lpErrors As Long, _
lpStat As COMSTAT) As Long

Public Const CE_BREAK = &H10 ' break condition
Public Const CE_PTO = &H200 ' printer timeout
Public Const CE_IOE = &H400 ' printer I/O error
Public Const CE_DNS = &H800 ' device not selected
Public Const CE_OOP = &H1000 ' out of paper

Luego en boton de comando
Private Sub btfunciona_Click()
Dim mHandle As Long
Dim lpErrors As Long
Dim x As COMSTAT

mHandle = CreateFile("lpt1", GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)

If mHandle < 0 Then
MsgBox "error de apertura del puerto " & mHandle
Else
If ClearCommError(mHandle, lpErrors, x) = False Then
MsgBox "Error en ClearCommError"
End If

If lpErrors And CE_BREAK Then
MsgBox "Error genérico"
End If

If lpErrors And CE_PTO Then
MsgBox "Timeout de impresora"
End If

If lpErrors And CE_IOE Then
MsgBox "Error de entrada/salida"
End If

If lpErrors And CE_DNS Then
MsgBox "Dispositivo no seleccionado" 'impresora apagada o algo por el estilo
End If

If lpErrors And CE_OOP Then
MsgBox "Sin papel"
End If

CloseHandle mHandle
End If
End Sub

Ya está Chau