Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 9, 2023 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/f31a5150b40d2b73d969b6fe889b81ba to your computer and use it in GitHub Desktop.
Save parzibyte/f31a5150b40d2b73d969b6fe889b81ba to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Text;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
public class ImpresoraTermica
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
public static void Main()
{
string nombreImpresora = "PT210";
string textoParaImprimir = "Hola impresora desde C#!";
string hostname = System.Environment.MachineName;
string ubicacionCompletaImpresora = string.Format("\\\\{0}\\{1}", System.Environment.MachineName, nombreImpresora);
SafeFileHandle fh = CreateFile(ubicacionCompletaImpresora, FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero);
if (fh.IsInvalid)
{
Console.WriteLine("Error abriendo impresora");
return;
}
var impresoraComoArchivo = new FileStream(fh, FileAccess.ReadWrite);
impresoraComoArchivo.WriteByte(0x1b); // ESC
impresoraComoArchivo.WriteByte(0x40); // @
impresoraComoArchivo.Write(Encoding.ASCII.GetBytes(textoParaImprimir), 0, textoParaImprimir.Length);
impresoraComoArchivo.WriteByte(0x1b); // Feed
impresoraComoArchivo.WriteByte(Convert.ToByte('d'));
impresoraComoArchivo.WriteByte(Convert.ToByte(1)); // 1 línea
impresoraComoArchivo.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment