locked
Sending Info to LPT Port from C# RRS feed

  • Question

  • I was wondering, if there is a way to send info straight to LPT port (printer attached to that port)??? I already know how to bring up the menu to select available printers, and I also know how to bypass that window, if I know the printers name. What I really would like to know, only knowing that there is LPT port, can I send the information to that port??? Thank you... Mike
    Tuesday, April 1, 2008 4:30 PM

Answers

  • Thanks guys.... I read this article... But here is code that really does what I wanted (just posting it here in case someone else will run ito this problem).....

    Code Snippet

        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();
                }
            }
        }

    Tuesday, April 1, 2008 8:38 PM

All replies

  •  

    You can, Levent Saltuklaroglu has written an excellent article on managing the parallel port on CodeProject, at http://www.codeproject.com/KB/cs/csppleds.aspx

    Tuesday, April 1, 2008 4:37 PM
  • Not through the Framework. You will need to use an external dll to do that kind of job. See this article for more information: http://www.codeproject.com/KB/cs/csppleds.aspx

     

    Tuesday, April 1, 2008 4:41 PM
  • Thanks guys.... I read this article... But here is code that really does what I wanted (just posting it here in case someone else will run ito this problem).....

    Code Snippet

        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();
                }
            }
        }

    Tuesday, April 1, 2008 8:38 PM