views:

49

answers:

1

My goal is to print labels with barcodes and a date stamp from an entry to a zebra TLP 2844 when the user clicks the ok button/hits enter. i found what i think might be the code for this from zebras site and have been integrating it into my program but part of it is depreciated and i cant quite figure out how to update it. below is what i have so far. The printer is attached via USB and the program will also store the entered numbers in a database but i have that part done. any help would be greatly Appreciated.

 
Public Class ScanForm
 
   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, <MarshalAs(UnmanagedType.Struct)> 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
 
 
 
 
  <StructLayout(LayoutKind.Sequential)> Public Structure SECURITY_ATTRIBUTES
 
       Private nLength As Integer
       Private lpSecurityDescriptor As Integer
       Private bInheritHandle As Integer
 
   End Structure
 
 
 
 
   Private Sub OKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKButton.Click
 
       Dim TrNum
       Dim TrDate
       Dim SA As SECURITY_ATTRIBUTES
       Dim outFile As FileStream, hPortP As IntPtr
 
       LPTPORT = "USB001"
       TrNum = Me.ScannedBarcodeText.Text()
       TrDate = Now()
 
       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) 'Create FileStream using Handle
       Dim fileWriter As New StreamWriter(outFile)
 
       fileWriter.WriteLine(" ")
       fileWriter.WriteLine("N")
       fileWriter.Write("A50,50,0,4,1,1,N,")
       fileWriter.Write(Chr(34))
       fileWriter.Write(TrNum) 'prints the tracking number variable
       fileWriter.Write(Chr(34))
       fileWriter.Write(Chr(13))
       fileWriter.Write(Chr(10))
       fileWriter.Write("A50,100,0,4,1,1,N,")
       fileWriter.Write(Chr(34))
       fileWriter.Write(TrDate) 'prints the date variable
       fileWriter.Write(Chr(34))
       fileWriter.Write(Chr(13))
       fileWriter.Write(Chr(10))
       fileWriter.WriteLine("P1")
       fileWriter.Flush()
       fileWriter.Close()
       outFile.Close()
       retval = CloseHandle(hPort)
 
       'Add entry to database
       Using connection As New SqlClient.SqlConnection("Data Source=MNGD-LABS-APP02;Initial Catalog=ScannedDB;Integrated Security=True;Pooling=False;Encrypt=False"), _
       cmd As New SqlClient.SqlCommand("INSERT INTO [ScannedDBTable] (TrackingNumber, Date) VALUES (@TrackingNumber, @Date)", connection)
           cmd.Parameters.Add("@TrackingNumber", SqlDbType.VarChar, 50).Value = TrNum
           cmd.Parameters.Add("@Date", SqlDbType.DateTime, 8).Value = TrDate
           connection.Open()
           cmd.ExecuteNonQuery()
           connection.Close()
       End Using
 
       'Prepare data for next entry
       ScannedBarcodeText.Clear()
       Me.ScannedBarcodeText.Focus()
 
   End Sub
 
+1  A: 

Change hPortP to a SafeHandle.

However, you should change your CreateFile declaration to return a SafeHandle instead. (and to take IntPtrs instead of Integers) Your current code will not work on x64.

SLaks
What would it take to make it x64? Is it a pitb?
VBPRIML
How also do I make this safehandle?
VBPRIML
Change `IntPtr` to `SafeHandle`.
SLaks
Thabks for your patience, that got the error, now the "hPortp =..." line above it says the value type intptr cannot be converted to safe handle.
VBPRIML
Change the variable to a `SafeHandle`.
SLaks
Now it is an overload resolution failure because no accessable new can be called with these arguments: value of safehandle cannot be converted to long and value of safe handle cannot be converted to integer
VBPRIML
So change the other arguments.
SLaks
I don't think that I can the error is "public sub new(value as long): value of type 'system.runtime.interopservices.safehandle cannot be converted to long" and "public sub new(value as integer): value of type 'system.runtime.interopservices.safehandle cannot be converted to integer"
VBPRIML
Don't make an `IntPtr` at all.
SLaks
What should I use so as not to bork it?
VBPRIML