苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5531|回复: 0

[其他] C#远程调用条码打印机的问题

[复制链接]
发表于 2014-4-25 09:46:06 | 显示全部楼层 |阅读模式
系统有一项功能是通过远程调用条码打印机(斑马打印机  型号:gk888t)来打印条码,使用C#开发的.将代码中的ip地址换成"."后在本地运行可以打印成功.可是通过远程访问其他机器上的打印机就没有反应, 经调试发现,在open方法中调用的CreateFile方法执行后返回的结果都是"-1"(应该是没有获取到操作设备的句柄的意思),  无法正确的打开远程机器上的usb设备的端口. 求大神指教..附上打印功能的代码(网上扒下来修改后的).

[C#] 纯文本查看 复制代码
        [StructLayout(LayoutKind.Sequential)]
        private struct OVERLAPPED
        {
            int Internal;
            int InternalHigh;
            int Offset;
            int OffSetHigh;
            int hEvent;
        }
        /// <summary>
        /// 打开端口
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern int CreateFile(
            string lpFileName,
            uint dwDesiredAccess,
            int dwShareMode,
            int lpSecurityAttributes,
            int dwCreationDisposition,
            int dwFlagsAndAttributes,
            int hTemplateFile);

        /// <summary>
        /// 发送命令
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern bool WriteFile(
            int hFile,
            byte[] lpBuffer,
            int nNumberOfBytesToWriter,
            out int lpNumberOfBytesWriten,
            out OVERLAPPED lpOverLapped);

        /// <summary>
        /// 关闭端口
        /// </summary>
        /// <param name="hObject"></param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(int hObject);

        /// <summary>
        /// 中文处理
        /// </summary>
        /// <param name="ChineseText">待转变中文内容</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="Orient">旋转角度0,90,180,270</param>
        /// <param name="Height">字体高度</param>
        /// <param name="Width">字体宽度,通常是0</param>
        /// <param name="IsBold">1 变粗,0 正常</param>
        /// <param name="IsItalic">1 斜体,0 正常</param>
        /// <param name="ReturnPicData">返回的图片字符</param>
        /// <returns></returns>
        [DllImport("FNTHEX32.dll", CharSet = CharSet.Ansi)]
        public static extern int GETFONTHEX(
            string ChineseText,
            string FontName,
            int Orient,
            int Height,
            int Width,
            int IsBold,
            int IsItalic,
            StringBuilder ReturnPicData);

        /// <summary>
        /// 中文处理
        /// </summary>
        /// <param name="ChineseText">待转变中文内容</param>
        /// <param name="FontName">字体名称</param>
        /// <param name="FileName">返回的图片字符重命</param>
        /// <param name="Orient">旋转角度0,90,180,270</param>
        /// <param name="Height">字体高度</param>
        /// <param name="Width">字体宽度,通常是0</param>
        /// <param name="IsBold">1 变粗,0 正常</param>
        /// <param name="IsItalic">1 斜体,0 正常</param>
        /// <param name="ReturnPicData">返回的图片字符</param>
        /// <returns></returns>
        [DllImport("FNTHEX32.dll", CharSet = CharSet.Ansi)]
        public static extern int GETFONTHEX(
                              string ChineseText,
                              string FontName,
                              string FileName,
                              int Orient,
                              int Height,
                              int Width,
                              int IsBold,
                              int IsItalic,
                              StringBuilder ReturnPicData);

        private int iHandle;
        //打开LPT 端口
        public bool Open(string PNPDeviceID)
        {
            string ss = "\\\\192.168.10.116\\" + PNPDeviceID.Replace('\\', '#') + "#{A5DCBF10-6530-11D2-901F-00C04FB951ED}";
            iHandle = CreateFile(ss, (uint)FileAccess.ReadWrite, 0, 0, (int)FileMode.Open, 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);
                bool b = WriteFile(iHandle, mybyte, mybyte.Length, out i, out x);
                return b;
            }
            else
            {
                throw new Exception("端口未打开~!");
            }
        }
        //关闭打印端口
        public bool Close()
        {
            return CloseHandle(iHandle);
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="strCommand"></param>
        public void ZPL_Print(string strCommand)
        {
            #region 远程打印

            //确定WMI操作的范围
            ConnectionOptions options = new ConnectionOptions();
            options.Username = "Administrator";//设定用于WMI连接操作的用户名
            options.Password = "1234";//设定用户的口令
            ManagementScope scope = new ManagementScope("\\\\192.168.10.116\\root\\cimv2", options);//修改查询范围
            scope.Connect();
            //确定WMI操作的内容
            SelectQuery selectQuery = new SelectQuery("Win32_USBHub");
            ManagementObjectSearcher query = new ManagementObjectSearcher(scope, selectQuery);
            #endregion

            //获取WMI操作内容
            ManagementObjectCollection queryCollection = query.Get();

            foreach (ManagementObject disk in queryCollection)
            {
                string PNPDeviceID = disk["PNPDeviceID"] as String;
                Open(PNPDeviceID);
                Write(strCommand);
                Close();
            }
        }


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2025-1-9 00:33

© 2014-2021

快速回复 返回顶部 返回列表