P
Palash Aich
Guest
I am using VB.Net code to print barcode label in Zebra TLP 2844 printer. And the printer is connected to system by USB port. When i execute the code i get an error "Invalid Parameter : Name handle" in the line outFile = New FileStream(_SafeFileHandle, FileAccess.Write) . Can you please help me to rectify this.
As this is a USB port So i shared the printer and used the UNC path. But still i get the error. Below is the complete code
Name Spaces
Imports System.IO
Imports System.Runtime.InteropServices
Here is the button click code
Dim _print as new ZebraPrint
_print.StartWrite("//ComputerName/PrinterSharedName")
dim _Text as String = "Print test"
_print.Write("A30,120,0,4,2,1,N,""" & _Text & """")
_print.EndWrite()
Class
Public Class ZebraPrint
#Region " Private constants "
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const OPEN_EXISTING As Integer = 3
#End Region
#Region " Private members "
Private _SafeFileHandle As Microsoft.Win32.SafeHandles.SafeFileHandle
Private _fileWriter As StreamWriter
Private _outFile As FileStream
#End Region
#Region " private structures "
<StructLayout(LayoutKind.Sequential)> _
Public Structure SECURITY_ATTRIBUTES
Private nLength As Integer
Private lpSecurityDescriptor As Integer
Private bInheritHandle As Integer
End Structure
#End Region
#Region " com calls "
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, <MarshalAs(UnmanagedType.Struct)> ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Microsoft.Win32.SafeHandles.SafeFileHandle
#End Region
#Region " Public methods "
Public Sub StartWrite(ByVal printerPath As String)
Dim SA As SECURITY_ATTRIBUTES
_SafeFileHandle = CreateFile(printerPath, GENERIC_WRITE, 0, SA, OPEN_EXISTING, 0, 0)
Try
_outFile = New FileStream(_SafeFileHandle, FileAccess.Write)
_fileWriter = New StreamWriter(_outFile)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("Can not find printer.", "Warning", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error, Windows.Forms.MessageBoxDefaultButton.Button1)
End Try
End Sub
Public Sub Write(ByVal rawLine As String)
If _fileWriter IsNot Nothing Then
_fileWriter.WriteLine(rawLine)
End If
End Sub
Public Sub EndWrite()
If _fileWriter IsNot Nothing Then
_fileWriter.Flush()
_fileWriter.Close()
_outFile.Close()
End If
_SafeFileHandle.Close()
_SafeFileHandle.Dispose()
End Sub
#End Region
End Class
Continue reading...
As this is a USB port So i shared the printer and used the UNC path. But still i get the error. Below is the complete code
Name Spaces
Imports System.IO
Imports System.Runtime.InteropServices
Here is the button click code
Dim _print as new ZebraPrint
_print.StartWrite("//ComputerName/PrinterSharedName")
dim _Text as String = "Print test"
_print.Write("A30,120,0,4,2,1,N,""" & _Text & """")
_print.EndWrite()
Class
Public Class ZebraPrint
#Region " Private constants "
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const OPEN_EXISTING As Integer = 3
#End Region
#Region " Private members "
Private _SafeFileHandle As Microsoft.Win32.SafeHandles.SafeFileHandle
Private _fileWriter As StreamWriter
Private _outFile As FileStream
#End Region
#Region " private structures "
<StructLayout(LayoutKind.Sequential)> _
Public Structure SECURITY_ATTRIBUTES
Private nLength As Integer
Private lpSecurityDescriptor As Integer
Private bInheritHandle As Integer
End Structure
#End Region
#Region " com calls "
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, <MarshalAs(UnmanagedType.Struct)> ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Microsoft.Win32.SafeHandles.SafeFileHandle
#End Region
#Region " Public methods "
Public Sub StartWrite(ByVal printerPath As String)
Dim SA As SECURITY_ATTRIBUTES
_SafeFileHandle = CreateFile(printerPath, GENERIC_WRITE, 0, SA, OPEN_EXISTING, 0, 0)
Try
_outFile = New FileStream(_SafeFileHandle, FileAccess.Write)
_fileWriter = New StreamWriter(_outFile)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("Can not find printer.", "Warning", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error, Windows.Forms.MessageBoxDefaultButton.Button1)
End Try
End Sub
Public Sub Write(ByVal rawLine As String)
If _fileWriter IsNot Nothing Then
_fileWriter.WriteLine(rawLine)
End If
End Sub
Public Sub EndWrite()
If _fileWriter IsNot Nothing Then
_fileWriter.Flush()
_fileWriter.Close()
_outFile.Close()
End If
_SafeFileHandle.Close()
_SafeFileHandle.Dispose()
End Sub
#End Region
End Class
Continue reading...