I know 2 forms of print on that way:
1- Printing to the LPT1 port Declare Auto Function CreateFile Lib "kernel32" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Int16, ByVal lpSecurityAttributes As Short, ByVal dwCreationDisposition As Int16, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Int16) As IntPtr Const Create_Always As Integer = 2 Const File_Flag_Overlapped As Integer = &H40000000 Const Generic_write As Integer = &H40000000 Const File_attribute_normal As Integer = &H80
Sub ImprimirLPT1(ByVal Cadena As String) Try Dim Hn As IntPtr Hn = CreateFile("lpt1:", Generic_write, 0, Nothing, Create_Always, File_attribute_normal Or File_Flag_Overlapped, Nothing) Dim Fs As New System.IO.FileStream(Hn, IO.FileAccess.Write) Dim i As Integer Dim S(Cadena.Length - 1) As Byte For i = 0 To S.Length - 1 S(i) = Asc(Cadena.Substring(i, 1)) Next Fs.Write(S, 0, S.Length) Fs.Flush() Fs.Close() Catch MsgBox(Err.Description) End Try End Sub
Call this function with the string you want to print
2.- Using API functions
Declare Function GetVersion Lib "kernel32" () As Integer Declare Auto Function OSOpenPrinterW Lib "winspool.drv" Alias "OpenPrinterW" (ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean Declare Auto Function MsluOpenPrinterW Lib "unicows.dll" Alias "OpenPrinterA" (ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean If (GetVersion() > &H80000000) Then MsgBox(GetVersion() & "OSPrinter") OpenPrinter = Me.OSOpenPrinterW(src, hPrinter, pd) Else MsgBox(GetVersion() & "MsluOpenPrinter") OpenPrinter = Me.MsluOpenPrinterW(src, hPrinter, pd) End If
End Function
Declare Auto Function OSStartDocPrinterW Lib "winspool.Drv" Alias "StartDocPrinterW" (ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As RawPrinterHelper.DOCINFOW) As Boolean Declare Auto Function MsluStartDocPrinterW Lib "unicows.dll" Alias "StartDocPrinterW" (ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As RawPrinterHelper.DOCINFOW) As Boolean Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean If (GetVersion() > &H80000000) Then
MsgBox(GetVersion() & "OSStartDocPrinterW") StartDocPrinter = OSStartDocPrinterW(hPrinter, level, pDI) Else MsgBox(GetVersion() & "MsluStartDocPrinterW") StartDocPrinter = MsluStartDocPrinterW(hPrinter, level, pDI) End If End Function
Declare Auto Function ClosePrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr) As Boolean Declare Auto Function EndDocPrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr) As Boolean Declare Auto Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Boolean Declare Auto Function EndPagePrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr) As Boolean Declare Auto Function WritePrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean Declare Auto Function GetLastError Lib "kernel32" () As Integer <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _ Structure DOCINFOW <MarshalAs(UnmanagedType.LPWStr)> Public pDocName As String <MarshalAs(UnmanagedType.LPWStr)> Public pOutputFile As String <MarshalAs(UnmanagedType.LPWStr)> Public pDataType As String End Structure
Function SendBytes(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean Dim hPrinter As IntPtr ' The printer handle. Dim dwError As Int32 ' Last error - in case there was trouble. Dim di As DOCINFOW ' Describes your document (name, port, data type). Dim dwWritten As Int32 ' The number of bytes written by WritePrinter(). Dim bSuccess As Boolean ' Your success code.
' Set up the DOCINFO structure. With di .pDocName = "Documento BMS" .pDataType = "RAW" End With ' Assume failure unless you specifically succeed. bSuccess = False Dim pd As New PrintDialog() ' You need a string to send. ' Open the printer dialog box, and then allow the user to select a printer. pd.PrinterSettings = New System.Drawing.Printing.PrinterSettings() 'If pd.ShowDialog(Me) Then If Me.OpenPrinter(pd.PrinterSettings.PrinterName, hPrinter, 0) Then If Me.StartDocPrinter(hPrinter, 1, di) Then If Me.StartPagePrinter(hPrinter) Then ' Write your printer-specific bytes to the printer. bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten) EndPagePrinter(hPrinter) End If Me.EndDocPrinter(hPrinter) End If Me.ClosePrinter(hPrinter) End If If bSuccess = False Then MsgBox("Error " & Me.GetLastError()) End If End Function
|