Hi There
I am trying to print to a DataMax label printer directly using the winspool.drv API's.
Everything seems fine, as I can connect to the printer and send everything through without getting any errors. I just have one problem and that is that the printer does not respond to the requests/data sent through to it.
Here is a copy of my code:
Declare Function GetVersion Lib "kernel32" () As Integer
Declare Auto Function OSOpenPrinterW Lib "winspool.drv" Alias "OpenPrinterW" (ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
Declare Auto Function MsluOpenPrinterW Lib "unicows.dll" Alias "OpenPrinterA" (ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
If (GetVersion() > &H80000000) Then
MsgBox(GetVersion() &
"OSPrinter")
OpenPrinter =
Me.OSOpenPrinterW(src, hPrinter, pd)
Else
MsgBox(GetVersion() &
"MsluOpenPrinter")
OpenPrinter =
Me.MsluOpenPrinterW(src, hPrinter, pd)
End If
End Function
Declare Auto Function OSStartDocPrinterW Lib "winspool.Drv" Alias "StartDocPrinterW" (ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
Declare Auto Function MsluStartDocPrinterW Lib "unicows.dll" Alias "StartDocPrinterW" (ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
If (GetVersion() > &H80000000) Then
MsgBox(GetVersion() &
"OSStartDocPrinterW")
StartDocPrinter = OSStartDocPrinterW(hPrinter, level, pDI)
Else
MsgBox(GetVersion() &
"MsluStartDocPrinterW")
StartDocPrinter = MsluStartDocPrinterW(hPrinter, level, pDI)
End If
End Function
Declare Auto Function ClosePrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr) As Boolean
Declare Auto Function EndDocPrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr) As Boolean
Declare Auto Function StartPagePrinter Lib "winspool.drv" (ByVal hPrinter As IntPtr) As Boolean
Declare Auto Function EndPagePrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr) As Boolean
Declare Auto Function WritePrinter Lib "winspool.Drv" (ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
Declare Auto Function GetLastError Lib "kernel32" () As Integer
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Structure DOCINFOW
<MarshalAs(UnmanagedType.LPWStr)>
Public pDocName As String
<MarshalAs(UnmanagedType.LPWStr)>
Public pOutputFile As String
<MarshalAs(UnmanagedType.LPWStr)>
Public pDataType As String
End Structure
Function SendBytes(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
Dim hPrinter As IntPtr ' The printer handle.
Dim dwError As Int32 ' Last error - in case there was trouble.
Dim di As DOCINFOW ' Describes your document (name, port, data type).
Dim dwWritten As Int32 ' The number of bytes written by WritePrinter().
Dim bSuccess As Boolean ' Your success code.
' Set up the DOCINFO structure.
With di
.pDocName =
"Documento BMS"
.pDataType =
"RAW"
End With
' Assume failure unless you specifically succeed.
bSuccess =
False
Dim pd As New PrintDialog()
' You need a string to send.
' Open the printer dialog box, and then allow the user to select a printer.
pd.PrinterSettings =
New System.Drawing.Printing.PrinterSettings()
'If pd.ShowDialog(Me) Then
If Me.OpenPrinter(pd.PrinterSettings.PrinterName, hPrinter, 0) Then
If Me.StartDocPrinter(hPrinter, 1, di) Then
If Me.StartPagePrinter(hPrinter) Then
' Write your printer-specific bytes to the printer.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
EndPagePrinter(hPrinter)
End If
Me.EndDocPrinter(hPrinter)
End If
Me.ClosePrinter(hPrinter)
End If
If bSuccess = False Then
MsgBox(
"Error " & Me.GetLastError())
End If
End Function
Regards
coenie