最近买了一台芯烨 的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); private void button1_Click(object sender, EventArgs e) { MsgBox(0, " 这就是用 DllImport 调用 DLL 弹出的提示框哦! ", " 挑战杯 ", 0x30); bool openPrint = uniInitNetSev(); if (openPrint) { MessageBox.Show("打印机初始化成功"); } else { MessageBox.Show("打印机初始化失败"); } int printState = uniConnectNetPortByIp("100.0.33.219", 9100, 5000); MessageBox.Show("打印机" + printState.ToString()); }