
10/10/2008, 18:23
|
| | Fecha de Ingreso: octubre-2008
Mensajes: 12
Antigüedad: 16 años, 6 meses Puntos: 0 | |
Respuesta: Ayuda para imprimir El siguiente código imprime un archivo directamente en la impresora que este conectada al puerto que especificas en el procedimiento ImpresionDirecta. A este
prodedimiento le pasas el nombre del archivo y el puerto.
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Class LPTWrite
<StructLayout(LayoutKind.Sequential)> _
Private Structure SECURITY_ATTRIBUTES
Public nLength As Integer
Public lpSecurityDescriptor As Integer
Public bInheritHandle As Integer
End Structure
Private Const GENERIC_WRITE As Integer = 1073741824
Private Const FILE_SHARE_WRITE As Integer = 2
Private Const OPEN_EXISTING As Integer = 3
Private Const FILE_ATTRIBUTE_NORMAL As Integer = 128
<DllImport("kernel32.dll")> _
Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, <MarshalAs(UnmanagedType.Struct)> _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
End Function
<STAThread()> _
Public Sub ImpresionDirecta(ByVal fil As String, ByVal Puerto As String)
Try
Dim srleedatos As StreamReader = New StreamReader(fil)
Dim sa As New SECURITY_ATTRIBUTES()
sa.bInheritHandle = 0
sa.lpSecurityDescriptor = 0
sa.nLength = Marshal.SizeOf(sa)
Dim hLPT1 As IntPtr = CreateFile(Puerto, GENERIC_WRITE, FILE_SHARE_WRITE, sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
Dim fs As New FileStream(DirectCast(hLPT1, IntPtr), FileAccess.Write)
Dim sw As New StreamWriter(fs)
sw.WriteLine(srleedatos.ReadToEnd)
sw.Flush()
fs.Flush()
fs.Close()
srleedatos.Close()
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString)
End Try
End Sub |