public static void LptPrint(String portName, String receiptText, String textEncoding) { IntPtr ptr = CreateFile(portName, GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (ptr.ToInt32() == -1) { int errorCode = Marshal.GetHRForLastWin32Error(); if ((errorCode == -2147024894) || (errorCode == -2147023888)) { throw new TextPrinterException(TextExceptionId.PortNameError); } else { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } } else { // musi być true bo inaczej drukuje wszystko oprócz ostatniej linii i dzieją się dziwne rzeczy Microsoft.Win32.SafeHandles.SafeFileHandle sfh = new Microsoft.Win32.SafeHandles.SafeFileHandle(ptr, true); FileStream lpt = new FileStream(sfh, FileAccess.ReadWrite); Byte[] buffer = new Byte[4096]; buffer = (MazoviaEncoding.GetEncoding(textEncoding)).GetBytes(receiptText); lpt.Write(buffer, 0, buffer.Length); lpt.Close(); } }
public static void Print(String printer, String text, String textEncoding) { //Console.WriteLine("printer=" + printer + "| textEncoding=" + textEncoding + "|"); //Console.WriteLine(text); byte[] dstBytes = (MazoviaEncoding.GetEncoding(textEncoding)).GetBytes(text); GCHandle pinnedArray = GCHandle.Alloc(dstBytes, GCHandleType.Pinned); IntPtr pointer = pinnedArray.AddrOfPinnedObject(); RawPrinterHelper.SendBytesToPrinter(printer, pointer, dstBytes.Length); pinnedArray.Free(); }