从 Windows 应用程序打印到 LPT1 端口

Printing from Windows application to LPT1 port

我正在 VS 2015 上构建一个 windows 应用程序,我需要它在点阵打印机上使用 LPT1 打印简单文本。 任何人都可以帮忙吗? 谢谢

我尝试应用以下代码,但它没有打印任何内容:

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()

与以下 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

我也试过第二种方法,但效果不佳

 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

与 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

我能够解决它,对于遇到这个问题的任何人,这里是有效的代码:

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