Invalid Parameter: Name handle Exception RRS feed

  • Question

  • 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
    dim _Text as String = "Print test"
    _print.Write("A30,120,0,4,2,1,N,""" & _Text & """")



     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)

      _SafeFileHandle = CreateFile(printerPath, GENERIC_WRITE, 0, SA, OPEN_EXISTING, 0, 0)


                    _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


                End If

            End Sub


            Public Sub EndWrite()


                If _fileWriter IsNot Nothing Then




                End If




            End Sub

    #End Region


        End Class
    Monday, November 23, 2009 12:21 PM

All replies

  • Hi,

    You might have better luck getting this question answered in one of the other forums, as this one primarily deals with the Pos .Net library, which you don't appear to be using.

    Alternatively, you could change your code to use Pos .Net instead, but I understand the Zebra service objects don't work very well with Pos .Net at the moment so you may be better off trying to fix what you have.

    Monday, November 23, 2009 8:55 PM
  • Hey Yort,
    I am disappointed a bit. Because i am in a critical situation for this issue as i am not getting any solution for this problem. Can you let me know which forum can i post the same to get a better answer.

    Tuesday, November 24, 2009 9:13 AM
  • Hi,

    I can understand that, but as I said, this forum is for the Pos .Net library which insulates developers from any port related work. As a result, devs using Pos .Net do not need to use the file API's, usb sub-system, serial ports etc. directly, and probably won't know enough to help unless they've worked with these things in some other capacity.

    I would suggest the Visual C# General forum, it's not a language or BCL issue since it involves the Win32 API and so I would guess that is the most appropriate place. It also has a large number of devs who know a variety of topics, so there's a much better chance someone there can help.

    Good luck !
    Tuesday, November 24, 2009 8:27 PM
  • Hi

    i saw your one of the old question.. and now i have also  the same error in my visual studio project.

    if u got any solution for the same . please share to me. 

    error "Invalid Parameter : Name handle" in the line outFile = New FileStream(_SafeFileHandle, FileAccess.Write) 


    Wednesday, December 2, 2020 3:57 PM
  • Hi

    Please post a new question on the new Microsoft Q&A forum

    Regards Les, Livingston, Scotland

    Wednesday, December 2, 2020 4:11 PM