0

I want to do a print of the dos program that supports lpt1 then I want to do the print by redirecting to the usb port and below the file path asc file. how can do a raw print of the asc file that is or with other solutions do directly or through a temp file that has the extension "tmp". Please the best solution. Thanks

my path asc file is C:\vDos\#LPT1.asc
my path temp file is C:\Users\Admin\AppData\Local\Temp\vDos
Public Class Form1
    Public Const FILE_ATTRIBUTE_NORMAL As Short = &H80
    Public Const INVALID_HANDLE_VALUE As Short = -1
    Public Const GENERIC_READ As UInteger = &H80000000UI
    Public Const GENERIC_WRITE As UInteger = &H40000000
    Public Const CREATE_NEW As UInteger = 1
    Public Const CREATE_ALWAYS As UInteger = 2
    Public Const OPEN_EXISTING As UInteger = 3

    <DllImport("kernel32.dll", SetLastError:=True)>
 Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As UInteger, ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As UInteger, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As IntPtr) As SafeFileHandle
    End Function

    Public Shared Sub sendTextToLPT1(ByVal receiptText As String)
        Using ptr As SafeFileHandle = CreateFile("LPT1", GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)

        ' Is bad handle? INVALID_HANDLE_VALUE 
         If ptr.IsInvalid Then
            ' ask the framework to marshall the win32 error code to an exception 
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error())
        Else
            Using lpt As New FileStream(ptr, FileAccess.ReadWrite)
            Dim buffer(2047) As Byte
            'Check to see if your printer support ASCII encoding or Unicode.
            'If unicode is supported, use the following:
            'buffer = System.Text.Encoding.Unicode.GetBytes(Temp);
            buffer = System.Text.Encoding.ASCII.GetBytes(receiptText)
            lpt.Write(buffer, 0, buffer.Length)
            lpt.Close()
            End Using
        End If
End Using
    End Sub

    Private Sub btnPrint_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim s As String = "C:\vDos\#LPT1.ASC"
        Dim p As New PrintDocument()
        AddHandler p.PrintPage, Sub(sender1 As Object, e1 As PrintPageEventArgs)
                                    e1.Graphics.DrawString(s, New Font("Times New Roman", 12), New SolidBrush(Color.Black), New RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height))
                                End Sub
        Try
            p.Print()
        Catch ex As Exception
            Throw New Exception("Exception Occured While Printing", ex)
        End Try
    End Sub
End Class

RESULT IN PRN FILE

Charlieface
  • 52,284
  • 6
  • 19
  • 43
roy
  • 693
  • 2
  • 11
  • `CreateFile("USB001"` maybe? Side note: Should be `Using Dim lpt As New FileStream(ptr, FileAccess.ReadWrite)` – Charlieface Jul 21 '22 at 15:27
  • @Charlieface , I have a warning `Public Sub New(handle As System.IntPtr, access As System.IO.FileAccess)' is obsolete: 'This constructor has been deprecated. Please use new FileStream(SafeFileHandle handle, FileAccess access) instead. http://go.microsoft.com/fwlink/?linkid=14202'.` and `Using Dim lpt As New FileStream(ptr, FileAccess.ReadWrite)` according to your code an error appears `Expression expected.` – roy Jul 22 '22 at 11:08
  • That's a separate point: You are creating the file handle but never closing it. Whereas if you change `CreateFile` to return `As SafeFileHandle` and then put that handle in a `Using` then it will close automatically at `End Using`. It won't solve your primary issue though. – Charlieface Jul 22 '22 at 11:10
  • @Charlieface , I updated the code according to your use `using` & `end using` but there is still a warning and I also updated the path of the asc file `Dim s As String = "C:\vDos\#LPT1.ASC"` but the output result in the prn file is not appropriate and attached in the screenshot as well – roy Jul 22 '22 at 11:28
  • You haven't added `SafeFileHandle` as I said, but whatever it's not relevant to your question. I'm confused about what you actually want. Are you trying to print to `LPT1` or to `USB001`? And why don't you just use the standard answer here for raw printing https://stackoverflow.com/questions/6911258/sending-data-to-usb-printer-in-c – Charlieface Jul 22 '22 at 11:32
  • @Charlieface , the position of the code that appears warning is in this line of code `Using lpt As New FileStream(ptr, FileAccess.ReadWrite)` and where i put the code `SafeFileHandle` . I tried to print from the dos program which lpt1 which is redirected to local or network printer which is usb – roy Jul 22 '22 at 11:45
  • You keep talking about a DOS porgram, which is not clear what it is or what it does, nor how the `C:\vDos\#LPT1.asc` file is relevant. If you want to print raw to the printer I gave you a link. – Charlieface Jul 22 '22 at 12:06
  • Your side issue with ` SafeFileHandle` is that `CreateFile` should be declared like this. `Private Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As UInteger, ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As UInteger, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As IntPtr) As SafeFileHandle` and the returned value of that needs a `Using` as well as the `FileStream` – Charlieface Jul 22 '22 at 12:06
  • @Charlieface , `'ToInt32' is not a member of 'Microsoft.Win32.SafeHandles.SafeFileHandle'.` I have updated the code accordingly from you. I have an error in the line code – roy Jul 22 '22 at 13:13
  • No it isn't a member, not sure why you thought it would be. The docs clearly show `SafeFileHandle.IsInvalid` though, which is what you should have used https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.safehandles.safefilehandle.isinvalid?view=net-6.0. You still don't have a `Using` on it though – Charlieface Jul 22 '22 at 13:15
  • `You keep talking about a DOS porgram, which is not clear what it is or what it does, nor how the C:\vDos\#LPT1.asc file is relevant. If you want to print raw to the printer I gave you a link.` actually I want to create an app like dosprinter – roy Jul 22 '22 at 13:19
  • @Charlieface , i've updated the code using `using` but still `'ToInt32' is not a member of 'Microsoft.Win32.SafeHandles.SafeFileHandle'.` – roy Jul 22 '22 at 13:30
  • As I said `If ptr.IsInvalid Then` – Charlieface Jul 22 '22 at 13:30
  • @Charlieface , I have updated the code from you and there is no error anymore – roy Jul 22 '22 at 13:40
  • @Charlieface , But I haven't managed the raw printing process of the asc file – roy Jul 22 '22 at 13:47

0 Answers0