最近在替公司做个条码打印的软件,之前是利用Ztools软件打印的,后来在网上了个组件,执行时只要输入数量超过10程序就会死掉,大家帮忙看看是什么问题组件代码如下:
class LPTControl
    {
        [StructLayout(LayoutKind.Sequential)]
        private struct OVERLAPPED
        {
            int Internal;
            int InternalHigh;
            int Offset;
            int OffSetHigh;
            int hEvent;
        }        [DllImport("kernel32.dll")]
        private static extern int CreateFile(
        string lpFileName,
        uint dwDesiredAccess,
        int dwShareMode,
        int lpSecurityAttributes,
        int dwCreationDisposition,
        int dwFlagsAndAttributes,
        int hTemplateFile
        );
        [DllImport("kernel32.dll")]
        private static extern bool WriteFile(
        int hFile,
        byte[] lpBuffer,
        int nNumberOfBytesToWrite,
        out   int lpNumberOfBytesWritten,
        out   OVERLAPPED lpOverlapped
        );
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(
        int hObject
        );        private int iHandle;
        public bool Open()
        {
            iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
            if (iHandle != -1)
            {
                return true;
            }
            else
            {
                return false;
            }        }
        public bool Write(String Mystring)
        {
            if (iHandle != -1)
            {
                int i;
                OVERLAPPED x;
                byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
                return WriteFile(iHandle, mybyte, mybyte.Length, out   i, out   x);
            }
            else
            {
                throw new Exception("端口未打开!");
            }
        }
        public bool Close()
        {
            return CloseHandle(iHandle);
        }    }
我调用组件的代码是:
    public partial class lxprint : Form
    {
        string t1 = "";
        string t2 = "";
        string t3 = "";
        string t4 = "";
        string t5 = "";
        string t6 = "";
        string t7 = "";
        string t8 = "";
        string t9 = "";
        string ta = "";
        string tb = "";
        public lxprint()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            LPTControl lpt = new LPTControl();
            
            t1 = this.comboBox1.Text.TrimEnd();
            t2 = this.comboBox2.Text.TrimEnd();
            t3 = this.textBox1.Text.TrimEnd();
            t4 = this.textBox2.Text.TrimEnd();
            t5=  this.textBox3.Text.TrimEnd();
            t7 = this.textBox4.Text.TrimEnd();
            int j=Convert.ToInt32(t4);
            int k = Convert.ToInt32(t3);
            for (int i = 0; i < j; i++)
            {
                t6="^XA^LH0,0^PRA"
                 +"^FO100,1^A0,28,21^FD"+t1+"^FS"
                 +"^FO10,25^BY2,,30^BC,,N,,,A^SN40"+t7+"94K"+k.ToString()+"5B,1,Y^FS"
                 +"^FO120,60^A0,28,21^FD40"+t7+"94K"+k.ToString()+"5C^FS"
                 +"^FO100,88^A0,28,21^FD"+t2+"^FS"
                +"^PQ1"
                +"^XZ";
                lpt.Open();
                lpt.Write(t6);
                lpt.Close();
                k++;

            }
            //this.button1.Visible = false;        }