Tenho um aplicativo para impressão de código de barras que funciona perfeitamente, entretanto iremos trocar a impressora que antes era paralela para uma nova usb (Zebra GK420t). Agora estou em dúvida de como adaptar meu software, tenho o código de impressão
abaixo:
public class PrintFactory
{
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;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
public static void sendTextToLPT1(String receiptText)
{
IntPtr ptr = CreateFile("LPT1", GENERIC_WRITE, 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
/* Is bad handle? INVALID_HANDLE_VALUE */
if (ptr.ToInt32() == -1)
{
/* ask the framework to marshall the win32 error code to an exception */
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
else
{
FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
Byte[] buffer = new Byte[2048];
//Check to see if your printer support ASCII encoding or Unicode.
//If unicode is supported, use the following:
//buffer = System.Text.Encoding.Unicode.GetBytes(Temp);
buffer = System.Text.Encoding.ASCII.GetBytes(receiptText);
lpt.Write(buffer, 0, buffer.Length);
lpt.Close();
}
}
}
Na linha onde tinha LPT1:
IntPtr ptr = CreateFile("LPT1", GENERIC_WRITE, 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
Mudei para USB001, mas não funcionou, na hora que clicava no botão "Imprimir" o software gerava o seguinte erro:
System.IO.FileNotFoundException: O sistema não pode encontrar o arquivo especificado. (Exceção de HRESULT: 0x80070002)
em System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
em System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
em CodigoBarras.PrintFactory.sendTextToLPT1(String receiptText)
Testei usar \\.\USB001 e também não funcionou, alguém teria alguma dica de como proceder?