VisualBasic.NET 将原始数据发送到打印机(.NET2005)

先前转载了同样的一 篇文章(通过 VisualBasic.NET 将原始数据发送到打印机  )但是此文章的代码只支持.net2003

以将原始数据发送到打印机从 Microsoft NET Framework , 程序必须使用 Win 32 后台函数。 通过 .NET Framework, 您可以通过使用 PrintDocument 、 PrintController , 和关联类打印。 但是, 使用 .NETFramework, 无法预设格式 - 打印机就绪数据发送到打印机。

您可能需要原始数据发送到打印机进行下列:

发送转义序列。
下载, 然后再使用软字体。
后台处理预打印文件。
以发送到打印机, 这些类型和其他类型的原始数据代码必须使用 Win 32 后台程序 Application 程序员接口 (API)。 以下代码显示如何通过 WritePrinter 然后发送到打印机那些字节读入内存, 预设格式文件的内容和。

注意 : 同一打印作业作为本机 PrintDocument 打印作业中无法使用此方法。 您必须要么使用 .NETFramework 要打印或发送自己的打印作业字节。

现在发表的这篇是支持.NET2005,源程序如下:

 

Imports  System.IO
Imports  System.ComponentModel
Imports  System.Windows.Forms
Imports  System.Drawing
Imports  System.Drawing.Printing
Imports  System
Imports  System.Runtime.InteropServices

Public   Class RawPrinterHelper
    
    
<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


  
<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", _
     SetLastError:
=True, CharSet:=CharSet.Unicode, _
     ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
  
Public Shared Function OpenPrinter(ByVal src As StringByRef hPrinter As IntPtr, ByVal pd As IntegerAs Boolean
  
End Function

    
<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterW", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       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.Unicode, _
       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.Unicode, _
       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.Unicode, _
       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.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function GetLastError() As Int32
    
End Function


    
    
Public Shared Function SendBytesToPrinter(ByVal szPrinterName As StringByVal 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.

        
        
With di
            .pDocName 
= "My Visual Basic .NET RAW Document"
            .pDataType 
= "RAW"
        
End With
        
        bSuccess 
= False
    
If OpenPrinter(szPrinterName, hPrinter, 0Then
      
If StartDocPrinter(hPrinter, 1, di) 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 bSuccess = False Then
            dwError 
= GetLastError()
        
End If
        
Return bSuccess
    
End Function
 ' SendBytesToPrinter()

    
    
Public Shared Function SendFileToPrinter(ByVal szPrinterName As StringByVal szFileName As StringAs Boolean
        
        
Dim fs As New FileStream(szFileName, FileMode.Open)
        
        
Dim br As New BinaryReader(fs)
        
        
Dim bytes(fs.Length) As Byte
        
Dim bSuccess As Boolean
        
        
Dim pUnmanagedBytes As IntPtr

        
        bytes 
= br.ReadBytes(fs.Length)
        
        pUnmanagedBytes 
= Marshal.AllocCoTaskMem(fs.Length)
        
        Marshal.Copy(bytes, 
0, pUnmanagedBytes, fs.Length)
        
        bSuccess 
= SendBytesToPrinter(szPrinterName, pUnmanagedBytes, fs.Length)
        
        Marshal.FreeCoTaskMem(pUnmanagedBytes)
        
Return bSuccess
    
End Function
 ' SendFileToPrinter()

   
    
Public Shared Function SendStringToPrinter(ByVal szPrinterName As StringByVal szString As String)
        
Dim pBytes As IntPtr
        
Dim dwCount As Int32
        
        dwCount 
= szString.Length()
        
        pBytes 
= Marshal.StringToCoTaskMemAnsi(szString)
        
        SendBytesToPrinter(szPrinterName, pBytes, dwCount)
        Marshal.FreeCoTaskMem(pBytes)
    
End Function

End Class

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值