dll 中含有unsigned char数组,被C#调用 -

  public class OPVcDll
    {
        #region 对USB接口的使用(PHILIPH卡)
        [DllImport("dcrf32.dll")]
        public static extern int dc_init(Int16 port, long baud);  //初试化
        [DllImport("dcrf32.dll")]
        public static extern short dc_exit(int icdev);
        [DllImport("dcrf32.dll")]
        public static extern short dc_reset(int icdev, uint sec);
        [DllImport("dcrf32.dll")]
        public static extern short dc_request(int icdev, char _Mode, ref uint TagType);
        [DllImport("dcrf32.dll")]
        public static extern short dc_card(int icdev, char _Mode, ref ulong Snr);
        [DllImport("dcrf32.dll")]
        public static extern short dc_halt(int icdev);
        [DllImport("dcrf32.dll")]
        public static extern short dc_anticoll(int icdev, char _Bcnt, ref ulong IcCardNo);
        [DllImport("dcrf32.dll")]
        public static extern short dc_beep(int icdev, uint _Msec);////蜂鸣
        [DllImport("dcrf32.dll")]
        public static extern short dc_authentication(int icdev, int _Mode, int _SecNr);

        [DllImport("dcrf32.dll")]
        public static extern short dc_load_key(int icdev, int mode, int secnr, [In] byte[] nkey);  //密码装载到读写模块中
        [DllImport("dcrf32.dll")]
        public static extern short dc_load_key_hex(int icdev, int mode, int secnr, string nkey);  //密码装载到读写模块中

        [DllImport("dcrf32.dll")]
        public static extern short dc_write(int icdev, int adr, [In] byte[] sdata);  //向卡中写入数据
        [DllImport("dcrf32.dll")]
        public static extern short dc_write(int icdev, int adr, [In] string sdata);  //向卡中写入数据
        [DllImport("dcrf32.dll")]
        public static extern short dc_write_hex(int icdev, int adr, [In] string sdata);  //向卡中写入数据(转换为16进制)

        [DllImport("dcrf32.dll")]
        public static extern short dc_read(int icdev, int adr, [Out] byte[] sdata);

        [DllImport("dcrf32.dll")]
        public static extern short dc_read(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata);  //从卡中读数据
        [DllImport("dcrf32.dll")]
        public static extern short dc_read_hex(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata);  //从卡中读数据(转换为16进制)
        [DllImport("dcrf32.dll")]
        public static extern int a_hex(string oldValue, ref string newValue, Int16 len);  //普通字符转换成十六进制字符
        [DllImport("dcrf32.dll")]
        public static extern void hex_a(ref string oldValue, ref string newValue, int len);  //十六进制字符转换成普通字符
        /// <summary>
        /// 读取卡信息  (unsigned char * )
        /// </summary>
        /// <param name="icdev"></param>
        /// <param name="_Snrlen"></param>
        /// <returns></returns>
        [DllImport("PdocMACDll.dll")]
        public static extern int mif_CPU_Card(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] _Snrlen);
        public static  int mif_CPU_Card(int icdev, ref byte[] _Snrlen)
        {
            return mif_CPU_Card(icdev, _Snrlen);
        }
        #endregion

        /// <summary>
        /// 初始化设备
        /// </summary>
        public static int Icdev
        {
            get
            {
                int dc_init = OPVcDll.dc_init(100, 115200);
                if (dc_init > 0)
                {
                    return dc_init;
                }
                else
                {
                    throw new Exception("初始化失败!");
                }

            }

        }

        /// <summary>
        /// 获取卡信息 
        /// </summary>
        /// <returns></returns>
        public static string ReadIc()
        {
            string cardId = "";
            int st;
            byte[] icCardNo = new byte[16];
            char tt = (char)1;
            uint ss = 0;
            int dc_init = Icdev;
            dc_beep((Int16)dc_init, 50);
            st =dc_request(dc_init, tt, ref ss); //检测卡
            if (st!=0)
            {
                 dc_exit(dc_init); //退出
                throw new Exception("获取卡信息出错!");
            }
            //是否为CPU卡
            if (ss != 8)
            {
                dc_exit(dc_init); //退出
                throw new Exception("该卡为非CPU卡不可以在本功能中发行!");
            }
                 
            mif_CPU_Card(dc_init, ref icCardNo);
            string str = Encoding.Default.GetString(icCardNo);
            if (str != null)
            {
                cardId = str;
            }
            else
            {
                dc_exit(dc_init); //退出
                throw new Exception("寻卡失败!");
            }
           
            return cardId;
        }

posted @ 2013-05-15 16:11  虫虫飞520  阅读(6508)  评论(1编辑  收藏  举报