public static bool SendBytesToPrinter(string szPrinterName, String pBytes, Int32 dwCount)
        {
            Int32 dwError = 0, dwWritten = 0;
            IntPtr hPrinter = new IntPtr();
            DOCINFO di = new DOCINFO();
            bool bSuccess = false; // Assume failure unless you specifically succeed.

            di.pDocName = "My C#.NET RAW Document";
            di.pDataType = "RAW";

            // Open the printer.
            if (PrintDirect.OpenPrinter(szPrinterName, ref hPrinter, 0))
            {
                // Start a document.
                if (PrintDirect.StartDocPrinter(hPrinter, 1, ref di))
                {
                    // Start a page.
                    if (PrintDirect.StartPagePrinter(hPrinter))
                    {
                        // Write your bytes.
                        bSuccess = PrintDirect.WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
                        PrintDirect.EndPagePrinter(hPrinter);
                    }
                    PrintDirect.EndDocPrinter(hPrinter);
                }
                PrintDirect.ClosePrinter(hPrinter);
            }
            // If you did not succeed, GetLastError may give more information
            // about why not.
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return bSuccess;
        }
 public void Close()
 {
     if (_PrintDevice == (int)PrinterDevice.Printer)
     {
         PrintDirect.EndPagePrinter(lhPrinter);
         PrintDirect.EndDocPrinter(lhPrinter);
         PrintDirect.ClosePrinter(lhPrinter);
     }
     else if ((_PrintDevice == (int)PrinterDevice.File))
     {
         fs.Close();
     }
 }
 public bool DeletePrinterConnection(String Printer)
 {
     int result;
     Global.ShowNotifier("Deleting printer : " + Printer);
     try
     {
         result = PrintDirect.DeletePrinterConnectionA(Printer);
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception:" + e.Message);
         return false;
     }
     //Console.Read();
     return true;
 }
        public void Open()
        {
            if (_PrintDevice == (int)PrinterDevice.Printer)
            {
                di.pDocName = _DocName;
                di.pDataType = "RAW";

                PrintDirect.OpenPrinter(_PrinterName, ref lhPrinter, 0);
                PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
                PrintDirect.StartPagePrinter(lhPrinter);
            }
            else if ((_PrintDevice == (int)PrinterDevice.File))
            {
                fs = File.Open(_FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            }


        }
 public  bool AddPrinterConnection(String Printer)
 {
     bool result;
     Global.ShowNotifier("Adding printer : " + Printer);
     try
     {
         //result = PrintDirect.AddPrinterConnection("\\\\sha-prn-01\\2017hp8100");
         //Console.WriteLine("result:" + result);
         result = PrintDirect.AddPrinterConnection(Printer);
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception:" + e.Message);
         return false;
     }
     //Console.Read();
     return true;
 }
        public bool Print(String Line)
        {
            try
            {
                if (_PrintDevice == (int)PrinterDevice.Printer)
                {
                    PrintDirect.WritePrinter(lhPrinter, Line, Line.Length, ref CharsWritten);
                }
                else if ((_PrintDevice == (int)PrinterDevice.File))
                {
                    Byte[] info = new UTF8Encoding(true).GetBytes(Line);

                    fs.Write(info, 0, info.Length);
                }
                else
                    Console.WriteLine(Line);

                if (Line.IndexOf(Control.CRLF) >= 0)
                {
                    _CurrentLine++;

                }
                if (Line.IndexOf(Control.FF) >= 0)
                {
                    _CurrentLine = 0;

                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            return true;
        }