我的目标是当用户点击确定按钮/点击输入时,打印带条形码的标签和从条目到斑马TLP 2844的日期戳。我发现了我认为可能是斑马网站的代码,并且已经将它集成到我的程序中,但是它的一部分是折旧的,我无法弄清楚如何更新它。以下是我到目前为止的情况。打印机通过USB连接,程序也会将输入的数字存储在数据库中,但我已完成该部分。任何帮助都会得到极大的赞赏。
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
答案 0 :(得分:1)
将hPortP
更改为SafeHandle
。
但是,您应该更改CreateFile
声明以返回SafeHandle
。 (并取IntPtr
代替Integer
s)
您当前的代码不适用于x64。