|
-
Apr 23rd, 2006, 02:24 PM
#1
Thread Starter
Addicted Member
[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.
-
Apr 24th, 2006, 01:21 AM
#2
Thread Starter
Addicted Member
Re: How to print text to LPT1: with VB.Net 2005
-
Apr 24th, 2006, 05:41 AM
#3
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:
Dim sfh As New Microsoft.Win32.SafeHandles.SafeFileHandle(fh, True)
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...
-
Apr 24th, 2006, 11:48 AM
#4
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?
-
Apr 24th, 2006, 11:42 PM
#5
Thread Starter
Addicted Member
Re: How to print text to LPT1: with VB.Net 2005
Thanks PAX,
Your code is working.
The Code I used is:
VB Code:
Dim fh As IntPtr
Dim SW As StreamWriter
Dim FS As FileStream
fh = Win32API.CreateFile("LPT1:", Win32API.GENERIC_WRITE, 0, 0, Win32API.CREATE_ALWAYS, 0, 0)
Dim sfh As New Microsoft.Win32.SafeHandles.SafeFileHandle(fh, True)
FS = New FileStream(sfh, FileAccess.Write)
FS.Flush()
SW = New StreamWriter(FS)
SW.WriteLine("Simple Text")
FS.Flush()
SW.Close()
FS.Close()
sfh.Close()
And the support class :
VB Code:
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
Thanks again to Pax
-
Apr 25th, 2006, 12:14 AM
#6
Thread Starter
Addicted Member
Re: How to print text to LPT1: with VB.Net 2005
 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.
-
Oct 30th, 2006, 03:24 AM
#7
Addicted Member
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:
Const a As String = "N" & vbCrLf & _
"B22,120,0,1,2,0,80,B," & """PUR001MSK200906000""" & vbCrLf & _
"I8,1,1" & vbCrLf & _
"A58,68,0,4,1,1,N," & """PT SEKAR BUMI Tbk.""" & vbCrLf & _
"A159,268,0,4,1,1,N," & """BOX ID""" & vbCrLf & _
"A76,352,0,4,1,1,N," & """" & vbCrLf & _
"A76,352,0,4,1,1,N," & """PUR001MSK200906000""" & vbCrLf & _
"P1"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|