Resumen Enviar datos a la impresora mediante API (winspool.drv)   (2 mensajes )

Hola Mario!!. Tan chico es el mundo que nos escribimos por mail estando
en la misma ciudad.
Te mando la información que vos pediste acerca de winspool.drv y un
ejemplo en VB (tomado de MSDN julio 98).

HOWTO: Send Raw Data to a Printer Using the Win32 API from VB

Last reviewed: January 15, 1998
Article ID: Q154078 The information in this article applies to:

•Standard, Professional, and Enterprise Editions of Microsoft Visual
Basic, 32-bit only, for Windows, version 4.0


SUMMARY

Although the printer object under Visual Basic 4.0 has been greatly
improved, there may still be times when it is desirable to use the Win
32 API to send information directly to the printer. Below is a code
sample showing how to achieve this behavior.





MORE INFORMATION



1.Start a new project in Visual Basic. Form1 is created by default.

2.Place a Command Button on the form.

3.Add the following code to the Form1 code window:





      Option Explicit







      Private Type DOCINFO

          pDocName As String
          pOutputFile As String
          pDatatype As String
      End Type

      Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal _
         hPrinter As Long) As Long
      Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal _
         hPrinter As Long) As Long
      Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal
_
         hPrinter As Long) As Long
      Private Declare Function OpenPrinter Lib "winspool.drv" Alias _
         "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As
Long, _
          ByVal pDefault As Long) As Long
      Private Declare Function StartDocPrinter Lib "winspool.drv" Alias
_
         "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As
Long, _
         pDocInfo As DOCINFO) As Long
      Private Declare Function StartPagePrinter Lib "winspool.drv"
(ByVal _
         hPrinter As Long) As Long
      Private Declare Function WritePrinter Lib "winspool.drv" (ByVal _
         hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, _
         pcWritten As Long) As Long

      Private Sub Command1_Click()
          Dim lhPrinter As Long
          Dim lReturn As Long
          Dim lpcWritten As Long
          Dim lDoc As Long
          Dim sWrittenData As String
          Dim MyDocInfo As DOCINFO
          lReturn = OpenPrinter("Call Screener Printer", lhPrinter, 0)
          If lReturn = 0 Then
              MsgBox "The Printer Name you typed wasn't recognized."
              Exit Sub
          End If
          MyDocInfo.pDocName = "AAAAAA"
          MyDocInfo.pOutputFile = vbNullString
          MyDocInfo.pDatatype = vbNullString
          lDoc = StartDocPrinter(lhPrinter, 1, MyDocInfo)
          Call StartPagePrinter(lhPrinter)
          sWrittenData = "How's that for Magic !!!!" & vbFormFeed
          lReturn = WritePrinter(lhPrinter, ByVal sWrittenData, _
             Len(sWrittenData), lpcWritten)
          lReturn = EndPagePrinter(lhPrinter)
          lReturn = EndDocPrinter(lhPrinter)
          lReturn = ClosePrinter(lhPrinter)
      End Sub








REFERENCES

The Win32 SDK Help files give a more detailed description of the APIs
used in the sample.

For more information, please see the following article in the Microsoft
Knowledge Base:

   ARTICLE-ID: Q138594
   TITLE : HOWTO: Send Raw Data to a Printer by Using the Win32 API

--
         Julio Daniel Moreyra
         Analista de Sistemas
   Sup. Tribunal de Justicia - Chubut

e-mail: jmoreyra@juschub.satlink.net
Rivadavia y Jones s/n - Rawson (9103) - Chubut - Argentina
Te: 54-0965-82331
Fax: 54-0965-82335

Mensaje enviado por "meletaes" <meletaes@yahoo.es> el 14/03/2002

Prueba con esto a ver cómo te va:
' Win32 API para mandar caracteres directamente al puerto de la impresora por defecto
'
Private Declare Function GetProfileString Lib "kernel32"
Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName
As Any, ByVal lpDefault As String, ByVal lpReturnedString As String,
ByVal nSize As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv"
Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrn As Long,
pDefault As Any) As Long
Private Declare Function StartDocPrinter Lib "winspool.drv"
Alias "StartDocPrinterA" (ByVal hprn As Long, ByVal Level As Long,
pDocInfo As DOC_INFO_1) As Long
Private Declare Function StartPagePrinter Lib "winspool.drv" (ByVal
hprn As Long) As Long
Private Declare Function WritePrinter Lib "winspool.drv" (ByVal hprn
As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long) As Long
Private Declare Function EndPagePrinter Lib "winspool.drv" (ByVal
hprn As Long) As Long
Private Declare Function EndDocPrinter Lib "winspool.drv" (ByVal hprn
As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hprn
As Long) As Long
Public Declare Function ShellExecute Lib "shell32.dll"
Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As
String, ByVal lpFile As String, ByVal lpParameters As String, ByVal
lpDirectory As String, ByVal nShowCmd As Long) As Long


'
' Estructura requerida para StartDocPrinter
'
Private Type DOC_INFO_1
   pDocName As String
   pOutputFile As String
   pDatatype As String
End Type

Dim bien As Long


Public Sub Iniciar_Spool(PrnName As String, hprn As Long)
'Abrir impresora, donde PRnName es el DeviceName de la impresora.Se
devuelve hprn como identificador
  Dim di As DOC_INFO_1
  di.pDocName = "Cadena"
  di.pOutputFile = vbNullString
  di.pDatatype = "RAW"
  Call OpenPrinter(PrnName, hprn, ByVal 0&)
  Call StartDocPrinter(hprn, 1, di)
  Call StartPagePrinter(hprn)
End Sub




Public Sub Fin_Spool(ByVal hprn As Long)
'Cerrar impresora
  Call EndPagePrinter(hprn)
  Call EndDocPrinter(hprn)
  Call ClosePrinter(hprn)
End Sub



Public Sub imprimir_Spool(ByVal strCadena As String, ByVal hprn As
Long, salto As Integer)
'Imprimir cadena

  Dim Buffer() As Byte
  Dim Written As Long
  Dim i As Long, j As Long
  Const BufSize As Long = &H50 '80 CARACTERES

  Dim BytesStr As Long
  Dim acopiar$
  Dim csalto$
  Dim cretorno$
  Dim veces As Integer
  Dim asciicadena As String

  asciicadena = cadena_ansi_a_ascii(strCadena)
  cretorno = Chr(10) 'Salto linea
  csalto = Chr(13) 'Retorno carro
  veces = 1
  If salto Then veces = 3
  Do While veces > 0
    If salto Then
      If veces = 3 Then 'Cadena
        acopiar = asciicadena
      Else
        If veces = 2 Then 'Salto
          acopiar = csalto
        Else 'Retorno
          acopiar = cretorno
        End If
      End If
    Else 'No salto
      acopiar = asciicadena
    End If
    BytesStr = ((Len(acopiar)) * 2) - 1
    ReDim Buffer(1 To BufSize) As Byte
    For i = 1 To UBound(Buffer)
      Buffer(i) = &H20 'Rellenar a blancos el buffer
    Next i
    Buffer() = acopiar$ 'Llenar el buffer
    Call WritePrinter(hprn, Buffer(0), BytesStr, Written) 'Imprimir

    veces = veces - 1
  Loop

End Sub

Ejemplo llamadas:
  Call Iniciar_Spool(prn.DeviceName, himp)
  Call imprimir_Spool(linea, himp, True) 'En linea van los
caracteres y codigos de escape
  Call Fin_Spool(himp)



        Un saludo
                      Miren



Resumen Resumen

Visual Basic Página de Visual Basic

Página principal Página principal

www.jrubi.com