Results 1 to 7 of 7

Thread: [RESOLVED] How to print text to LPT1: with VB.Net 2005

  1. #1

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    Resolved [RESOLVED] How to print text to LPT1: with VB.Net 2005

    Hi all programmers,


    I want to print some text to LPT1: port attached to a Dot Matrix Printer in VB.Net 2005 like
    Print "Hello World" to LPT1:


    I have done this in VB.Net 2002 using Win32 API and File Stream.
    (The link for it is :http://www.vbforums.com/showthread.php?t=253270 )

    This cocept is not working in VB.Net 2005. The reason is function
    FileStream(intPtr, ...) version is outdated in VB.Net 2005


    Can anybody tell me how to do it in VB.Net 2005

    Thanks

    Parminder
    Last edited by parminder; Apr 24th, 2006 at 01:24 AM.

  2. #2

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    Re: How to print text to LPT1: with VB.Net 2005


  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Re: How to print text to LPT1: with VB.Net 2005

    I have no idea if this works, but have you tried using the new SafeHandle?

    VB Code:
    1. Dim sfh As New Microsoft.Win32.SafeHandles.SafeFileHandle(fh, True)
    2.         Dim fs As IO.FileStream = New IO.FileStream(sfh, IO.FileAccess.ReadWrite)
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: How to print text to LPT1: with VB.Net 2005

    Is there a reason you dont want to use a normal print document and .NET printing methods?

  5. #5

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    Re: How to print text to LPT1: with VB.Net 2005

    Thanks PAX,

    Your code is working.

    The Code I used is:

    VB Code:
    1. Dim fh As IntPtr
    2.         Dim SW As StreamWriter
    3.         Dim FS As FileStream
    4.         fh = Win32API.CreateFile("LPT1:", Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)
    5.         Dim sfh As New Microsoft.Win32.SafeHandles.SafeFileHandle(fh, True)
    6.         FS = New FileStream(sfh, FileAccess.Write)
    7.         FS.Flush()
    8.         SW = New StreamWriter(FS)
    9.         SW.WriteLine("Simple Text")
    10.         FS.Flush()
    11.         SW.Close()
    12.         FS.Close()
    13.         sfh.Close()

    And the support class :

    VB Code:
    1. Public Class Win32API
    2.     Public Const GENERIC_WRITE = &H40000000
    3.     Public Const CREATE_ALWAYS = 2
    4.     Public Const OPEN_EXISTING = 3
    5.     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
    6.     Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
    7. End Class


    Thanks again to Pax

  6. #6

    Thread Starter
    Addicted Member parminder's Avatar
    Join Date
    Apr 2003
    Location
    India
    Posts
    168

    Re: How to print text to LPT1: with VB.Net 2005

    Quote Originally Posted by gigemboy
    Is there a reason you dont want to use a normal print document and .NET printing methods?
    I am sending simple text (escape codes can be used) to different Dot Matrix Printers attached to one machine simultaneously and what text send to which printer is decided by software depending user's inputs. Using this I can use LPT1:,LPT2:,LPT3:, COM1:,COM2:,COM3:,COM4:,... simultaneously
    There is not matter which printer is default printer. In fact, this type of printing is driver-independent (for text with optional escape codes for dos style)
    This allow me to print one text-set on LPT1: which is near to computer and mean time software is printing second text-set to serial printer attached to COM1: (The distance of second printer from computer is 15 meter)

    Even parrerel port LPTn: allow me to print text at distance of 15 feet.

    ... and it is fast to print text as in DOS style applications.

    So the reasons is "Fast, Simulataneous and Printer-driver independent printing"
    Last edited by parminder; Apr 25th, 2006 at 12:18 AM.

  7. #7
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    Re: [RESOLVED] How to print text to LPT1: with VB.Net 2005

    Hi, Im currently works on a project just like parminder.

    Here's my code:
    VB Code:
    1. Const a As String = "N" & vbCrLf & _
    2.             "B22,120,0,1,2,0,80,B," & """PUR001MSK200906000""" & vbCrLf & _
    3.             "I8,1,1" & vbCrLf & _
    4.             "A58,68,0,4,1,1,N," & """PT SEKAR BUMI Tbk.""" & vbCrLf & _
    5.             "A159,268,0,4,1,1,N," & """BOX ID""" & vbCrLf & _
    6.             "A76,352,0,4,1,1,N," & """" & vbCrLf & _
    7.             "A76,352,0,4,1,1,N," & """PUR001MSK200906000""" & vbCrLf & _
    8.             "P1"
    9.  
    10.  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Dim fh As IntPtr
    12.         Dim SW As IO.StreamWriter
    13.         Dim FS As IO.FileStream
    14.         fh = Win32API.CreateFile("LPT1:", Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)
    15.         'This one works fine
    16.         'fh = Win32API.CreateFile("C:\test.txt", Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)
    17.         Dim sfh As New Microsoft.Win32.SafeHandles.SafeFileHandle(fh, True)
    18.         FS = New IO.FileStream(sfh, IO.FileAccess.ReadWrite)
    19.         FS.Flush()
    20.         SW = New IO.StreamWriter(FS)
    21.         SW.WriteLine(a)
    22.         FS.Flush()
    23.         SW.Close()
    24.         FS.Close()
    25.         sfh.Close()
    26.     End Sub

    The problem is, I got an error said
    ArgumentExepction was unhandled
    Invalid handle
    Parameter name: handle.

    Can someone help me with this problem ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width