Article View: pl.comp.lang.vbasic
Article #104340Re: wysłać plik na LPT
From: Jarek Mielcarek
Date: Thu, 15 Oct 2009 09:28
Date: Thu, 15 Oct 2009 09:28
206 lines
8580 bytes
8580 bytes
Jędruś pisze:
> Witam.
> Mam plik przygotowany do druku (*.prn) ale nie wiem jak go wysłać na
> drukarkę podłączoną do portu LPT
>
> Rozwiązania szukam dla VB6 i VB2008
>
> Jędrek
Dla vb2005/2008
Wywolanie kodu:
Dim _RPH As New RawPrinterHelper
_RPH.DocumentName = "jakas nazwa"
RawPrinterHelper.SendFileToPrinter("nazwa drukarki pobrana z Windows",
sciezka_do_pliku_do_wydruku)
_RPH = Nothing
Kod klasy:
Imports System.IO
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Public Class RawPrinterHelper
Shared xDocumentName As String = "XXX"
Public Structure PRINTER_DEFAULTS
Dim pDatatype As Integer 'String I"VE TRIED BOTH
Dim pDevMode As Integer
Dim DesiredAccess As Integer
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Class DOCINFOA
<MarshalAs(UnmanagedType.LPStr)> Public pDocName As String
<MarshalAs(UnmanagedType.LPStr)> Public pOutputFile As String
<MarshalAs(UnmanagedType.LPStr)> Public pDataType As String
End Class
Public WriteOnly Property DocumentName() As String
Set(ByVal Value As String)
xDocumentName = Value
End Set
End Property
' Structure and API declarions:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
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
<DllImport("winspool.Drv", EntryPoint:="OpenPrinterA",
SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function OpenPrinter(<MarshalAs(UnmanagedType.LPStr)>
ByVal szPrinter As String, ByRef hPrinter As IntPtr, ByVal pd As Long)
As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As
Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterA",
SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr,
ByVal level As Int32, <MarshalAs(UnmanagedType.LPStruct)> ByVal di As
DOCINFOA) As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As
Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr)
As Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As
Boolean
End Function
<DllImport("winspool.Drv", EntryPoint:="WritePrinter", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal
pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As
Boolean
End Function
<DllImport("kernel32.dll", EntryPoint:="GetLastError", _
SetLastError:=True, CharSet:=CharSet.Auto, _
ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function GetLastError() As Int32
End Function
Public Shared Function SendBytesToPrinter(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.
Dim pd As PRINTER_DEFAULTS
Dim dia As DOCINFOA = New DOCINFOA
With dia
.pDocName = xDocumentName
.pDataType = "RAW"
.pOutputFile = Nothing
End With
With pd
.DesiredAccess = 8
.pDatatype = 0
.pDevMode = 0
End With
' Set up the DOCINFO structure.
With di
.pDocName = xDocumentName
.pDataType = "RAW"
End With
' Assume failure unless you specifically succeed.
bSuccess = False
'If OpenPrinter(szPrinterName, hPrinter, 0) Then
If OpenPrinter(szPrinterName, hPrinter, 0) Then
'If StartDocPrinter(hPrinter, 1, di) Then
If StartDocPrinter(hPrinter, 1, dia) Then
If StartPagePrinter(hPrinter) Then
' Write your printer-specific bytes to the printer.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount,
dwWritten)
EndPagePrinter(hPrinter)
End If
EndDocPrinter(hPrinter)
End If
ClosePrinter(hPrinter)
End If
' If you did not succeed, GetLastError may give more information
' about why not.
If bSuccess = False Then
dwError = Marshal.GetLastWin32Error()
End If
Return bSuccess
End Function ' SendBytesToPrinter()
Public Shared Function SendFileToPrinter(ByVal szPrinterName As
String, ByVal szFileName As String) As Boolean
' Open the file.
Dim fs As New FileStream(szFileName, FileMode.Open)
' Create a BinaryReader on the file.
Dim br As New BinaryReader(fs)
' Dim an array of bytes large enough to hold the file's contents.
Dim bytes(fs.Length) As Byte
Dim bSuccess As Boolean
' Your unmanaged pointer
Dim pUnmanagedBytes As IntPtr
bytes = br.ReadBytes(fs.Length)
pUnmanagedBytes = Marshal.AllocCoTaskMem(fs.Length)
Marshal.Copy(bytes, 0, pUnmanagedBytes, fs.Length)
' Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes,
fs.Length)
' Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes)
fs.Close()
fs = Nothing
Return bSuccess
End Function ' SendFileToPrinter()
Public Shared Function SendStringToPrinter(ByVal szPrinterName As
String, ByVal szString As String) As Boolean
Dim pBytes As IntPtr
Dim dwCount As Int32
dwCount = szString.Length()
pBytes = Marshal.StringToCoTaskMemAuto(szString)
SendBytesToPrinter(szPrinterName, pBytes, dwCount)
Marshal.FreeCoTaskMem(pBytes)
End Function
End Class
Technical Details
Message-ID:
<hb6pvs$l35$1@news.onet.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!not-for-mailReferences:
<94ebbbe3-7751-49e3-992c-75ec3d30eef2@j4g2000yqa.googlegroups.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!not-for-mail From: Jarek Mielcarek <jarekmie@brak.mi.wp.pl> Newsgroups: pl.comp.lang.vbasic Subject: Re: wysłać plik na LPT Date: Thu, 15 Oct 2009 11:28:26 +0200 Organization: http://onet.pl Lines: 221 Message-ID: <hb6pvs$l35$1@news.onet.pl> References: <94ebbbe3-7751-49e3-992c-75ec3d30eef2@j4g2000yqa.googlegroups.com> NNTP-Posting-Host: jzq18.internetdsl.tpnet.pl Mime-Version: 1.0 X-Trace: news.onet.pl 1255598908 21605 95.50.42.18 (15 Oct 2009 09:28:28 GMT) X-Complaints-To: niusy@onet.pl NNTP-Posting-Date: Thu, 15 Oct 2009 09:28:28 +0000 (UTC) User-Agent: Thunderbird 2.0.0.23 (Windows/20090812) In-Reply-To: <94ebbbe3-7751-49e3-992c-75ec3d30eef2@j4g2000yqa.googlegroups.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit