I have a printer installed on another machine on the LPT1 port, and it is shared. I am trying to print a text from another machine with this method, I go the way of ip "\\ XXX.XXX.X.XXX \ namePrint" in CreateFile but the printer is always invalid.
I would be grateful if you can help me.
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.IO;
using System.Text;
using System.Data;
namespace WebTesteImpressora.ControlClass
{
public class PrintTexto
{
private const short FILE_ATTRIBUTE_NORMAL = 0x80;
private const short INVALID_HANDLE_VALUE = -1;
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const uint CREATE_NEW = 1;
private const uint CREATE_ALWAYS = 2;
private const uint OPEN_EXISTING = 3;
[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(
string lpFileName,
FileAccess dwDesiredAccess,
uint dwShareMode,
IntPtr lpSecurityAttributes,
FileMode dwCreationDisposition,
uint dwFlagsAndAttributes,
IntPtr hTemplateFile);
/// <summary>
/// Metodo utilizado para imprimir direto para a impressora conectada a porta serial
/// </summary>
/// <param name="texto"></param>
/// <param name="centralizar"></param>
/// <param name="qtdQuebra"></param>
/// <param name="arquivo"></param>
/// <param name="fim"></param>
///
public static void imprimir()
{
String text = "Test Print!";
Byte[] buffer = new byte[text.Length];
buffer = System.Text.Encoding.ASCII.GetBytes(text);
String path = @"//192.168.1.118/cupom";
SafeFileHandle printer = CreateFile(path, FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
if (printer.IsInvalid)
{
throw new System.Exception("Printer not found " + caminho);
}
else
{
FileStream lpt1 = new FileStream(printer, FileAccess.ReadWrite);
lpt1.Write(buffer, 0, buffer.Length);
lpt1.Close();
}
}
}
}