Sending text to lpt1

DotNetVirgin

New member
Joined
May 9, 2003
Messages
2
In VB6 using the following code, sends the printer control characters and text string to the printer device connected to lpt1.

Open "lpt1" For Output As #1
Print #1, "<RC350,315><FL10><X4>: Send this string to printer"
Close #1

I am trying to recreate this functionality in vb.net. From what I have managed to find on the subject, which is not a lot, it is implied that you can use the Stream class to send data to a parallel port. However I cant find out exactly how to do this.

Thank for any help you can give.
 
Youll need to make a call to the Win32 API function CreateFile which returns an IntPtr that can be used in the constructer of a FileStream object.
 
Thanks for your help - here is the code I used in case anybody else is looking for it!


Code:
Imports System.IO 
Imports System.Runtime.InteropServices 

Public Class Form1 
Inherits System.Windows.Forms.Form 
Public Const GENERIC_WRITE = &H40000000 
Public Const OPEN_EXISTING = 3 
Public Const FILE_SHARE_WRITE = &H2 

Dim LPTPORT As String 
Dim hPort As Integer 

Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _ 
ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, _ 
ByVal dwShareMode As Integer, _ 
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _ 
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 Integer) As Integer 
Dim retval As Integer 

Public Structure SECURITY_ATTRIBUTES 
Private nLength As Integer 
Private lpSecurityDescriptor As Integer 
Private bInheritHandle As Integer 
End Structure 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
Dim Texxxt As String 
Dim SA As SECURITY_ATTRIBUTES 
Dim outFile As FileStream, hPortP As IntPtr 

LPTPORT = "LPT1" 
Texxxt = Me.RichTextBox1.Text 
hPort = CreateFile(LPTPORT, GENERIC_WRITE, FILE_SHARE_WRITE, SA, OPEN_EXISTING, 0, 0) 

hPortP = New IntPtr(hPort) convert Integer to IntPtr 
outFile = New FileStream(hPortP, FileAccess.Write, False) Create FileStream using Handle 

Dim fileWriter As New StreamWriter(outFile) 
MessageBox.Show(RichTextBox1.Text) 
 fileWriter.AutoFlush = False 
fileWriter.WriteLine(RichTextBox1.Text) 
fileWriter.WriteLine("Hello World2") 
fileWriter.WriteLine("Hello World1") 
fileWriter.WriteLine("Hello World2") 
fileWriter.Write(Chr(12)) 12 
fileWriter.Flush() 
fileWriter.Close() 
outFile.Close() 

retval = CloseHandle(hPort) 
End Sub 
End Class
 
Last edited by a moderator:
Hi,

I am making an application in Net wherein the data is to be send directly to a barcode printer.

I tried the script above by DotNetVirgin (thanks by the way) but I got an invalid handle value of (-1). I am using a shared printer port.

Can you (anyone) please help me?

And also, do you know how to directly send the Cofiguration Settings to the printer?

Thanks! :)
 
Back
Top