0

I tried to print but the result of the printing status did not print immediately and the print time was approximately 10-12 seconds whether there is a solution so that it can be printed immediately or there is a need to be in the settings on my form

thanks

Public Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
        Dim dwError As Int32 = 0, dwWritten As Int32 = 0
        Dim hPrinter As New IntPtr(0)
        Dim di As New DOCINFOA()
        Dim bSuccess As Boolean = False ' Assume failure unless you specifically succeed.
        di.pDocName = "OUTPUTPRN"
        di.pDataType = "RAW"

        ' Open the printer.
        If OpenPrinter(szPrinterName.Normalize(), hPrinter, IntPtr.Zero) Then
            ' Start a document.
            If StartDocPrinter(hPrinter, 1, di) Then
                ' Start a page.
                If StartPagePrinter(hPrinter) Then
                    ' Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
                    EndPagePrinter(hPrinter)
                End If
                EndDocPrinter(hPrinter)
            End If
            ClosePrinter(hPrinter)
        End If
        ' If you did not succeed, GetLastError may give more information
        ' about why not.
        If bSuccess = False Then
            dwError = Marshal.GetLastWin32Error()
        End If
        Return bSuccess
    End Function
Public Shared Function SendFileToPrinter(ByVal printerName As String, ByVal filePath As String) As Boolean
        Dim bytes = File.ReadAllBytes(filePath).ToArray
        Dim byteCount = bytes.Length
        Dim unmanagedBytesPointer = Marshal.AllocCoTaskMem(byteCount)

        Marshal.Copy(bytes, 0, unmanagedBytesPointer, byteCount)

        Dim success = SendBytesToPrinter(printerName, unmanagedBytesPointer, byteCount)

        Marshal.FreeCoTaskMem(unmanagedBytesPointer)

        Return success
    End Function
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.close = True Then
Dim printer As String = "EPSON LX-300+II ESC/P"
Dim path As String = "C:\vDos\#LPT1.asc"
            For i As Integer = 1 To 1
                SendFileToPrinter(printer, path)
            Next i
            Me.Close()
        End If
    End Sub
roy
  • 693
  • 2
  • 11
  • 1
    Since we don't know what `SendBytesToPrinter` does you're going to have to profile your code yourself to learn where it's spending its time. – IInspectable Aug 04 '22 at 14:01
  • 3
    It depends on how the printer is configured in the OS, not on your code. If the printer is set to "Start printing after last page is spooled", you're gonna wait and no code change will help you. If the printer is set to "Start printing immediately" or "Print directly to the printer", things are usually pretty good. Receipt printers and other feed printers should pretty much always use "Print directly to the printer". – Joel Coehoorn Aug 04 '22 at 14:02
  • @IInspectable , I've updated my code maybe you can help me – roy Aug 04 '22 at 14:08
  • @JoelCoehoorn , Can it be set form load to so fast? or need modification or addition of code – roy Aug 04 '22 at 14:09
  • 1
    You shouldn't change this print spooler setting in your application *without* consulting your sysadmin(s). It's an OS-wide setting, i.e. it affects _all_ printing from that machine. Besides the fact that there might be reasons why it is configured the way it is, an application should never ever change _system_ settings *without* user consent. – Hel O'Ween Aug 04 '22 at 14:28
  • @HelO'Ween , if i do print directly from notepad or word then it prints directly so the problem with my code – roy Aug 04 '22 at 14:51
  • I suggest you use [Stopwatch](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch?view=net-6.0) to calculate program running time and find the slow position. `Dim watch As New Diagnostics.Stopwatch watch.Start() watch.Stop() Console.WriteLine(watch.Elapsed.TotalMilliseconds)` – Junjie Zhu - MSFT Aug 05 '22 at 07:14
  • @NickWestgate: I know. But the setting for a _specific_ printer it still affects the behavior for that printer on the _whole system_. – Hel O'Ween Aug 05 '22 at 08:54

0 Answers0