Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/03/2011, 16:24
lvwrz
Usuario no validado
 
Fecha de Ingreso: mayo-2005
Ubicación: Benicàssim
Mensajes: 56
Antigüedad: 19 años, 6 meses
Puntos: 0
Pregunta Impresion Matricial en Visual Studio 2010

Estimad@s tod@s,
Encontré un artículo que fue tratado en 2007, obvio, VB2010 no existía, ahora quiero aplicar este mismo código y tengo 2 adventencia que me impiden continual la ejecución.
Alguien sabe que debo cambiar para que sea compatible en VB2010?


PROBLEMA #1: Línea 115
Mensaje recibido:
Advertencia 1
La variable 'di' se ha pasado como referencia antes de haberle asignado un valor. Podría producirse una excepción de referencia nula en tiempo de ejecución. Asegúrese de que la estructura o todos los miembros de referencia se hayan inicializado antes de que se utilicen.


PROBLEMA #2: Línea 177
Mensaje Recibido:
Advertencia 2
La función 'SendStringToPrinter' no devuelve un valor en todas las rutas de acceso de código. Puede producirse una excepción de referencia nula en tiempo de ejecución cuando se use el resultado.


CODIGO COMPLETO:


Código vb:
Ver original
  1. Imports System.IO
  2. Imports System.Drawing.Printing
  3. Imports System.Runtime.InteropServices
  4.  
  5. Public Class Form1
  6.     Inherits System.Windows.Forms.Form
  7.  
  8.  
  9.     ' Click event handler for a button - designed to show how to use the
  10.    ' SendFileToPrinter and SendBytesToPrinter functions.
  11.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  12.         ' Allow the user to select a file.
  13.        Dim ofd As New OpenFileDialog
  14.         If ofd.ShowDialog(Me) Then
  15.             ' Allow the user to select a printer.
  16.            Dim pd As New PrintDialog
  17.             pd.PrinterSettings = New PrinterSettings
  18.             If (pd.ShowDialog() = DialogResult.OK) Then
  19.                 ' Print the file to the printer.
  20.                RawPrinterHelper.SendFileToPrinter(pd.PrinterSettings.PrinterName, ofd.FileName)
  21.             End If
  22.         End If
  23.     End Sub ' Button1_Click()
  24.  
  25.     ' Click event handler for a button - designed to show how to use the
  26.    ' SendBytesToPrinter function to send a string to the printer.
  27.    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  28.         Dim s As String
  29.         Dim pd As New PrintDialog
  30.         Dim cont As Integer = 0
  31.         Dim o As Integer = 0
  32.         Dim array(10) As String
  33.         ' You need a string to send.
  34.        s = "Hello, this is a test"
  35.         ' Open the printer dialog box, and then allow the user to select a printer.
  36.        pd.PrinterSettings = New PrinterSettings
  37.         If (pd.ShowDialog() = DialogResult.OK) Then
  38.             While cont <> 10
  39.                 array(cont) = "hola holita " & cont
  40.                 cont = cont + 1
  41.             End While
  42.             o = 0
  43.  
  44.             s = Chr(10) & array(o) + vbNewLine + array(o + 1) + vbNewLine + array(o + 2) + vbNewLine + array(o + 3) + vbNewLine + array(o + 4) + vbNewLine + array(o + 5) '& Chr(16)
  45.            RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s)
  46.  
  47.  
  48.         End If
  49.     End Sub ' Button2_Click()
  50. End Class
  51. Public Class RawPrinterHelper
  52.     ' Structure and API declarions:
  53.    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
  54.     Structure DOCINFOW
  55.         <MarshalAs(UnmanagedType.LPWStr)> Public pDocName As String
  56.         <MarshalAs(UnmanagedType.LPWStr)> Public pOutputFile As String
  57.         <MarshalAs(UnmanagedType.LPWStr)> Public pDataType As String
  58.     End Structure
  59.  
  60.     <DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", _
  61.     SetLastError:=True, CharSet:=CharSet.Unicode, _
  62.     ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  63.     Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
  64.     End Function
  65.     <DllImport("winspool.Drv", EntryPoint:="ClosePrinter", _
  66.     SetLastError:=True, CharSet:=CharSet.Unicode, _
  67.     ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  68.     Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
  69.     End Function
  70.     <DllImport("winspool.Drv", EntryPoint:="StartDocPrinterW", _
  71.     SetLastError:=True, CharSet:=CharSet.Unicode, _
  72.     ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  73.     Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
  74.     End Function
  75.     <DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", _
  76.     SetLastError:=True, CharSet:=CharSet.Unicode, _
  77.     ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  78.     Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
  79.     End Function
  80.     <DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", _
  81.     SetLastError:=True, CharSet:=CharSet.Unicode, _
  82.     ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  83.     Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
  84.     End Function
  85.     <DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", _
  86.     SetLastError:=True, CharSet:=CharSet.Unicode, _
  87.     ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  88.     Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
  89.     End Function
  90.     <DllImport("winspool.Drv", EntryPoint:="WritePrinter", _
  91.     SetLastError:=True, CharSet:=CharSet.Unicode, _
  92.     ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
  93.     Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
  94.     End Function
  95.  
  96.     ' SendBytesToPrinter()
  97.    ' When the function is given a printer name and an unmanaged array of
  98.    ' bytes, the function sends those bytes to the print queue.
  99.    ' Returns True on success or False on failure.
  100.    Public Shared Function SendBytesToPrinter(ByVal szPrinterName As String, ByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
  101.         Dim hPrinter As IntPtr ' The printer handle.
  102.        Dim dwError As Int32 ' Last error - in case there was trouble.
  103.        Dim di As DOCINFOW ' Describes your document (name, port, data type).
  104.        Dim dwWritten As Int32 ' The number of bytes written by WritePrinter().
  105.        Dim bSuccess As Boolean ' Your success code.
  106.  
  107.         ' Set up the DOCINFO structure.
  108.        With di
  109.             .pDocName = "My Visual Basic .NET RAW Document"
  110.             .pDataType = "RAW"
  111.         End With
  112.         ' Assume failure unless you specifically succeed.
  113.        bSuccess = False
  114.         If OpenPrinter(szPrinterName, hPrinter, 0) Then
  115.             If StartDocPrinter(hPrinter, 1, di) Then
  116.                 If StartPagePrinter(hPrinter) Then
  117.                     ' Write your printer-specific bytes to the printer.
  118.                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
  119.                     EndPagePrinter(hPrinter)
  120.                 End If
  121.                 EndDocPrinter(hPrinter)
  122.             End If
  123.             ClosePrinter(hPrinter)
  124.         End If
  125.         ' If you did not succeed, GetLastError may give more information
  126.        ' about why not.
  127.        If bSuccess = False Then
  128.             dwError = Marshal.GetLastWin32Error()
  129.         End If
  130.         Return bSuccess
  131.     End Function ' SendBytesToPrinter()
  132.  
  133.     ' SendFileToPrinter()
  134.    ' When the function is given a file name and a printer name,
  135.    ' the function reads the contents of the file and sends the
  136.    ' contents to the printer.
  137.    ' Presumes that the file contains printer-ready data.
  138.    ' Shows how to use the SendBytesToPrinter function.
  139.    ' Returns True on success or False on failure.
  140.    Public Shared Function SendFileToPrinter(ByVal szPrinterName As String, ByVal szFileName As String) As Boolean
  141.         ' Open the file.
  142.        Dim fs As New FileStream(szFileName, FileMode.Open)
  143.         ' Create a BinaryReader on the file.
  144.        Dim br As New BinaryReader(fs)
  145.         ' Dim an array of bytes large enough to hold the file's contents.
  146.        Dim bytes(fs.Length) As Byte
  147.         Dim bSuccess As Boolean
  148.         ' Your unmanaged pointer.
  149.        Dim pUnmanagedBytes As IntPtr
  150.  
  151.         ' Read the contents of the file into the array.
  152.        bytes = br.ReadBytes(fs.Length)
  153.         ' Allocate some unmanaged memory for those bytes.
  154.        pUnmanagedBytes = Marshal.AllocCoTaskMem(fs.Length)
  155.         ' Copy the managed byte array into the unmanaged array.
  156.        Marshal.Copy(bytes, 0, pUnmanagedBytes, fs.Length)
  157.         ' Send the unmanaged bytes to the printer.
  158.        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, fs.Length)
  159.         ' Free the unmanaged memory that you allocated earlier.
  160.        Marshal.FreeCoTaskMem(pUnmanagedBytes)
  161.         Return bSuccess
  162.     End Function ' SendFileToPrinter()
  163.  
  164.     ' When the function is given a string and a printer name,
  165.    ' the function sends the string to the printer as raw bytes.
  166.    Public Shared Function SendStringToPrinter(ByVal szPrinterName As String, ByVal szString As String)
  167.         Dim pBytes As IntPtr
  168.         Dim dwCount As Int32
  169.         ' How many characters are in the string?
  170.        dwCount = szString.Length()
  171.         ' Assume that the printer is expecting ANSI text, and then convert
  172.        ' the string to ANSI text.
  173.        pBytes = Marshal.StringToCoTaskMemAnsi(szString)
  174.         ' Send the converted ANSI string to the printer.
  175.        SendBytesToPrinter(szPrinterName, pBytes, dwCount)
  176.         Marshal.FreeCoTaskMem(pBytes)
  177.     End Function
  178. End Class

Muchas gracias a tod@s de antemano!!!