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