健康一贴灵,专注医药行业管理信息化

c# 调用DLL(开发小票打印机)

最近买了一台芯烨 的80宽度网络小票打印机,计划做一个类似后厨的实时接单打印程序,即每接到一个订单,系统就自动打印小票,驱动配货人员尽快工作。

厂家提供了DLL文件,一切从0开始摸索。

首先是要引入DLL,使用DllImport,指定引用的DLL名称,方式,编码格式等 ;

[DllImport("user32.dll", EntryPoint = "MessageBoxA")]
        static extern int MsgBox(int hWnd, string msg, string caption, int type);

        [DllImport("JsPrinterDll.dll", EntryPoint = "uniInitNetSev",CharSet =CharSet.Ansi,CallingConvention =CallingConvention.StdCall)]
        static extern bool  uniInitNetSev();

        [DllImport("JsPrinterDll.dll", EntryPoint = "uniConnectNetPortByIp", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        static extern int uniConnectNetPortByIp(string ip, int port = 9100, int Timeout = 5000);
        [DllImport("JsPrinterDll.dll", EntryPoint = "uniPrintImg1b2a", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        static extern int uniPrintImg1b2a(int fs,string imgpath);

        private void button1_Click(object sender, EventArgs e)
        {

            bool openPrint = uniInitNetSev();
            if (!openPrint) 
            { 
                MessageBox.Show("打印机初始化失败");
                return;
            }

            int printState = uniConnectNetPortByIp("100.1.1.219", 9100, 5000);
            if (printState <= 0) return;


            int nret = uniPrintImg1b2a(printState,"test.bmp");
            if (nret <= 0)
            {
                return;
            }
            MessageBox.Show("测试打印OK", nret.ToString());
        }

 

 

posted @ 2023-04-03 09:35  一贴灵  阅读(664)  评论(0编辑  收藏  举报
学以致用,效率第一