JaJa, si funciona siempre es un avance!
Igual, siempre es mejor controlar el proceso
 
Saludos
Esteban

  _____  

From: [email protected] [mailto:[email protected]] On Behalf Of
[email protected]
Sent: Viernes, 16 de Enero de 2009 17:01
To: vbnet List Member
Subject: [vbnet] impresion directa


Gracias por las respuestas.
 
No se rian de mi pobre implementación, pero funciona, y por ahi la pueden
aplicar para otra cosa similar; instancion un system.diagnostics.process y
le digo al cmd que imprima.
Saludos
 
Dim p As New Process
Dim pi As New ProcessStartInfo
pi.Arguments = "/c type c:\pepe.txt>lpt1"
pi.FileName = "cmd"
pi.UseShellExecute = False
p.StartInfo = pi
p.Start()

 
 
 


-----Original Message-----
From: "Carlos Peix" <[email protected]>
To: "vbnet List Member"  <[email protected]>
Date: Fri, 16 Jan 2009 17:14:28 -0200
Subject: [vbnet] impresion directa


Hola Estebanquito, 

Gracias por el codigo, a mi tambien me sirve.

Veo que estas haciendo un dispose sobre la referencia "hnd" del handle en el
catch pero no lo haces dentro del try. En otras palabras, te diria que tenes
un leak (ese dispose seguramente se hace, pero cuando el GC se lleva a la
instancia de UnmanagedFileLoader).

Yo haria dos cosas:

- Elimina la variable hnd y usa directamente ul.Handle para crear el stream
- Hace un ul.Handle.Dispose(); como ultima linea dentro del try y en el
catch.

Buen fin de semana.

Carlos Peix
[email protected]
tel: 4257-4622
cel: 15-4406-7571

-----Mensaje original-----
De: [email protected] [mailto:[email protected]] En nombre de Esteban A.
Zibecchi (MUG)
Enviado el: Viernes, 16 de Enero de 2009 05:53 p.m.
Para: vbnet List Member
Asunto: [vbnet] impresion directa

Nosotros escribimos directamente en el port. Es una clase simple y chica en
c#. 
Te pego el codigo.
 
            public bool PrintLine(string stringToPrint, int cantidadLineas)
            {
            try{
                    SafeFileHandle hnd = null;
                    try
                    {
                        UnmanagedFileLoader ul = new
UnmanagedFileLoader(lptPort);
                        hnd = ul.Handle;

                        using (FileStream stream = new FileStream(hnd,
FileAccess.Write))
                        {
                            //Grabo
                            char[] data = stringToPrint.ToCharArray();
                            byte[] dataBytes = new byte[data.Length];
                            for (int i = 0; i < data.Length; i++) {
dataBytes[i] = Convert.ToByte(data[i]); }
                            stream.Write(dataBytes, 0, dataBytes.Length);
                            for(int j = 0;j <  cantidadLineas ;j++)
                            {
                                stream.Write(new Byte[] { 13, 10 } , 0, 2);
                            }
                            //Lo Cierro
                            stream.Close();
                        }
                        System.Threading.Thread.Sleep(200);
                    }
                    catch 
                    {
                        if (hnd != null) hnd.Dispose();
                    }

               return true;
            }
            catch(Exception ex){
               Error.GetInstance().ErrorLog(ex ,
"Imprimiendo Linea en CF LPT" , 
                  new
ErrorBase.ErrorInfo("Source" , "CFiscalLPT.PrintLine") , 
                  new
ErrorBase.ErrorInfo("Linea" , stringToPrint)
               );
               return false;
            }
         } 
 
/////////////////////
Esta es la clase que envia el codigo.//////////////// /////////////////////

        private class UnmanagedFileLoader
        {

            public const short FILE_ATTRIBUTE_NORMAL = 0x80;
            public const short INVALID_HANDLE_VALUE = -1;
            public const uint GENERIC_READ = 0x80000000;
            public const uint GENERIC_WRITE = 0x40000000;
            public const uint CREATE_NEW = 1;
            public const uint CREATE_ALWAYS = 2;
            public const uint OPEN_EXISTING = 3;

            // Use interop to call the CreateFile function.
            // For more information about CreateFile,
            // see the unmanaged MSDN reference library.
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern SafeFileHandle CreateFile(string lpFileName, uint
dwDesiredAccess,
              uint dwShareMode, IntPtr lpSecurityAttributes, uint
dwCreationDisposition,
              uint dwFlagsAndAttributes, IntPtr hTemplateFile);



            private SafeFileHandle handleValue = null;


            public UnmanagedFileLoader(string Path)
            {
                Load(Path);
            }


            public void Load(string Path)
            {
                if (Path == null && Path.Length == 0)
                {
                    throw new ArgumentNullException("Path");
                }

                // Try to open the file.
                handleValue = CreateFile(Path, GENERIC_WRITE, 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

                // If the handle is invalid,
                // get the last Win32 error 
                // and throw a Win32Exception.
                if (handleValue.IsInvalid)
                {
 
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
                }
            }

            public SafeFileHandle Handle
            {
                get
                {
                    // If the handle is valid,
                    // return it.
                    if (!handleValue.IsInvalid)
                    {
                        return handleValue;
                    }
                    else
                    {
                        return null;
                    }
                }

            }

        }
Saludos
Esteban

________________________________

From: [email protected] [mailto:[email protected]] On Behalf Of
[email protected]
Sent: Viernes, 16 de Enero de 2009 15:22
To: vbnet List Member
Subject: [vbnet] impresion directa



 Hola, vuelvo a consultar sobre un problema que me parece increiblemente
difícil de resolver en .Net.
 
Simplemente quiero mandar a impresora un archivo de texto, pero en forma
directa, ya que contiene comandos de impresion (creo que no se puede
resolver con system.drawing.printing ).
Gracias





Responder a