I'm building a windows application on VS 2015 and I need it to print simple text using LPT1 on Dot-Matrix printer. anyone can help? Thank you
Asked
Active
Viewed 968 times
-1
-
What did your search of the topic reveal? Where are you stuck? – JimmyB May 17 '17 at 09:15
-
Question unclear: do you want to write a printer driver that prints using LPT1, or do you want to write code that sends data to an existing windows printer driver (that prints via LPT1), or do you want to bypass windows printing methods and send your data directly to LPT1? – Harald Coppoolse May 17 '17 at 10:02
-
Harald Coppoolse, i need to bypass windows printing methods and send my data directly to LPT1 – Hamoudi May 17 '17 at 10:10
-
I would like to mention that when using the second method and setting the printer to xps, a file is being saved as oxps and xps is unable to open it – Hamoudi May 17 '17 at 13:11
2 Answers
0
i tried to apply the following code but its not printing anything:
Dim fh As IntPtr
Dim SW As IO.StreamWriter
Dim FS As IO.FileStream
fh = Win32API.CreateFile("LPT1:", Win32API.GENERIC_WRITE, 0, 0,
Win32API.CREATE_ALWAYS, 0, 0)
This one works fine
fh = Win32API.CreateFile("C:\test.txt", Win32API.GENERIC_WRITE, 0, 0,
Win32API.CREATE_ALWAYS, 0, 0)
Dim sfh As New Microsoft.Win32.SafeHandles.SafeFileHandle(fh, True)
FS = New IO.FileStream(sfh, IO.FileAccess.ReadWrite)
FS.Flush()
SW = New IO.StreamWriter(FS)
SW.WriteLine(a)
FS.Flush()
SW.Close()
FS.Close()
sfh.Close()
with the following class
Public Class Win32API
Public Const GENERIC_WRITE = &H40000000
Public Const CREATE_ALWAYS = 2
Public Const OPEN_EXISTING = 3
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal
lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As
Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition
As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As
Integer) As Integer
Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle"
(ByVal hObject As Long) As Long
End Class
i also tried a second method but it didn't work as well
Private prn As New RawPrinterHelper
Dim prtSettg As New PrinterSettings()
Private PrinterName As String = prtSettg.PrinterName
Public Sub StartPrint()
prn.OpenPrint(PrinterName)
End Sub
Public Sub PrintHeader()
Print("Simple text")
End Sub
Public Sub Print(Line As String)
prn.SendStringToPrinter(PrinterName, Line + vbLf)
End Sub
Public Sub EndPrint()
prn.ClosePrint()
End Sub
Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) _
Handles btnCancel.Click
prn.ClosePrint()
Me.Close()
End Sub
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) _
Handles btnPrint.Click
StartPrint()
If prn.PrinterIsOpen = True Then
PrintHeader()
EndPrint()
End If
End Sub
with the class:
Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.IO
Public Class RawPrinterHelper
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Class DOCINFOA
<MarshalAs(UnmanagedType.LPStr)> _
Public pDocName As String
<MarshalAs(UnmanagedType.LPStr)> _
Public pOutputFile As String
<MarshalAs(UnmanagedType.LPStr)> _
Public pDataType As String
End Class
<DllImport("winspool.Drv", EntryPoint:="OpenPrinterA", _
SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(<MarshalAs(UnmanagedType.LPStr)> _
szPrinter As String, ByRef hPrinter As IntPtr, pd As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function ClosePrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterA", _
SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartDocPrinter(hPrinter As IntPtr, level As Int32, _
<[In](), MarshalAs(UnmanagedType.LPStruct)> di As DOCINFOA) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndDocPrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartPagePrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndPagePrinter(hPrinter As IntPtr) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="WritePrinter", _
SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function WritePrinter(hPrinter As IntPtr, pBytes As IntPtr, _
dwCount As Int32, ByRef dwWritten As Int32) As Boolean
End Function
Private hPrinter As New IntPtr(0)
Private di As New DOCINFOA()
Private PrinterOpen As Boolean = False
Public ReadOnly Property PrinterIsOpen As Boolean
Get
PrinterIsOpen = PrinterOpen
End Get
End Property
Public Function OpenPrint(szPrinterName As String) As Boolean
If PrinterOpen = False Then
di.pDocName = ".NET RAW Document"
di.pDataType = "RAW"
If OpenPrinter(szPrinterName.Normalize(), hPrinter, IntPtr.Zero) Then
If StartDocPrinter(hPrinter, 1, di) Then
If StartPagePrinter(hPrinter) Then
PrinterOpen = True
End If
End If
End If
End If
OpenPrint = PrinterOpen
End Function
Public Sub ClosePrint()
If PrinterOpen Then
EndPagePrinter(hPrinter)
EndDocPrinter(hPrinter)
ClosePrinter(hPrinter)
PrinterOpen = False
End If
End Sub
Public Function SendStringToPrinter(szPrinterName As String, szString As String) As Boolean
If PrinterOpen Then
Dim pBytes As IntPtr
Dim dwCount As Int32
Dim dwWritten As Int32 = 0
dwCount = szString.Length
pBytes = Marshal.StringToCoTaskMemAnsi(szString)
SendStringToPrinter = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
Marshal.FreeCoTaskMem(pBytes)
Else
SendStringToPrinter = False
End If
End Function
End Class

Hamoudi
- 41
- 3
0
I was able to solve it, and for anyone that face this issue, here is the code that worked:
Public Class PrintHelper
Friend TextToBePrinted As String
Dim prn As New Printing.PrintDocument
Public Sub prt(ByVal text As String, ByVal printer As String)
TextToBePrinted = text
Using (prn)
prn.PrinterSettings.PrinterName = printer
AddHandler prn.PrintPage, _
AddressOf Me.PrintPageHandler
prn.Print()
RemoveHandler prn.PrintPage, _
AddressOf Me.PrintPageHandler
End Using
End Sub
Private Sub PrintPageHandler(ByVal sender As Object, _
ByVal args As Printing.PrintPageEventArgs)
Dim myFont As New Font("Courier New", 9)
args.Graphics.DrawString(TextToBePrinted, _
New Font(myFont, FontStyle.Regular), _
Brushes.Black, 50, 50)
End Sub
End Class

Hamoudi
- 41
- 3