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
Printing from Windows application to LPT1 port
1k Views Asked by Hamoudi At
2
There are 2 best solutions below
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
I was able to solve it, and for anyone that face this issue, here is the code that worked: