ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#] LPT1 포트를 이용한 프린터 출력하기
    Programming/C# 2011. 3. 24. 19:01
    프린트 포트를 직접 제어하여 데이터 출력하는 방법입니다.

    출처 : http://blog.naver.com/PostView.nhn?blogId=bejjang19&logNo=30012783070 

    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace TEST
    {
        class LptPrint
        {
            const uint GENERIC_READ = 0x80000000;
    
            const uint GENERIC_WRITE = 0x40000000;
    
            const uint OPEN_EXISTING = 3;
    
            IntPtr handle;
    
            [DllImport("kernel32", SetLastError=true)]
    
            static extern unsafe IntPtr CreateFile(
    
                string FileName, //file name
    
                uint DesiredAccess, //access mode
    
                uint ShareMode, //share mode
    
                uint SecurityAttributes, //security attributes
    
                uint CreationDisposition, //how to create
    
                uint FlagsAndAttributes, //file attributes
    
                int hTemplateFile //handle to template file
    
                );
    
     
    
            [DllImport("kernel32", SetLastError=true)]
    
            static extern unsafe bool ReadFile(
    
                IntPtr hFile,
    
                void* pBuffer,
    
                int NumberOfBytesToRead,
    
                int* pNumberOfBytesRead,
    
                int Overlapped
    
                );
    
     
    
            [DllImport("kernel32", SetLastError=true)]
    
            static extern unsafe bool WriteFile(
    
                IntPtr hFile,
    
                void* pBuffer,
    
                int nBytesToWrite,
    
                int* nBytesWritten,
    
                int Overlapped
    
                );
    
     
    
            [DllImport("kernel32", SetLastError=true)]
    
            static extern int GetLastError();
    
     
    
            [DllImport("kernel32", SetLastError=true)]
    
            static extern unsafe bool CloseHandle(IntPtr hObject);
    
     
    
            public LptPrint()
            {
                //
                // TODO: Add constructor logic here
                //
            }
    
            public bool Open(string FileName)
            {
                handle = CreateFile( FileName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
    
                if(handle != IntPtr.Zero)
                    return true;
                else
                    return false;
            }
    
            public unsafe int Write(string cont)
            {
                int i=0,n=0;
                int Len = cont.Length;
    
                ASCIIEncoding e = new ASCIIEncoding();
    
                byte[] Buffer = e.GetBytes(cont);
    
                fixed (byte* p = Buffer)
                {
                    i=0;
    
                    if(!WriteFile(handle,p+i,Len+1,&n,0))
                        return 0;
                }
                return n;
            }
    
            public bool Close()
            {
                return CloseHandle(handle);
            }
        }
    
        public class PrintTool
        {
            public PrintTool()
            {
            }
    
            public int SendToPrint(string printer, string buffer)
            {
                LptPrint lp = new LptPrint();
                int iR = 0;
    
                if(lp.Open(printer))
                {
                    iR = lp.Write(buffer);
                }
    
                lp.Close();
    
                return iR;
            }
        }
    }
    



    시리얼 통신할때는 SerialPort에 대한 객체를 선언한 뒤에 Protname//baudrate//stopbit etc 를 맞춘뒤
    SerialPort.Write(buf); 형식을 보낸다

    컴파일 방법은..

    시작 -> 모든프로그램 -> Microsoft .Net Framework SDK v2.0 -> SDK 프롬프트 를 실행하신뒤

    csc /target:library /unsafe [파일명.cs]

    로 컴파일하신 후

    생성된 DLL를 참조하시면 됩니다.

    (VS.NET에서는 컴파일이 안되더군요... /unsafe옵션 어쩌구 하면서...)



    하지만 LPT1을 이용하여 프린터 할경우에는
    PrintTool pt = new PrintTool();
    pt.SendToPrint(@"C:\LPT1:", buf);

    에 대한 형식으로 진행하면 시리얼을 통해서 출력하는 결과와 같은 효과를 얻을 수 있다.
Designed by Tistory.