using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;  //调用动态库一定要加入这个引用

namespace iccardreader
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Label label1;
  /// <summary>
  /// 必需的设计器变量。
  //常量定义
  public const byte BLOCK0_EN = 0x01;//操作第0块
  public const byte BLOCK1_EN = 0x02;//操作第1块
  public const byte BLOCK2_EN = 0x04;//操作第2块
  public const byte NEEDSERIAL = 0x08;//仅对指定序列号的卡操作
  public const byte EXTERNKEY = 0x10;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button6;//用函数参数中的密码组进行读写卡时的认证,而不是用藏于读写器的芯片内的密码来认证。
  public const byte NEEDHALT = 0x20;//读卡或写卡后顺便休眠该卡,休眠后,卡必须拿离开感应区,再放回感应区,才能进行第二次操作。


  //------------------------------------------------------------------------------------------------------------------------------------------------------
  //外部函数声明:让设备发出声响
  [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdbeep")]
  static extern byte pcdbeep(ulong xms);//xms单位为毫秒

  //------------------------------------------------------------------------------------------------------------------------------------------------------   
  //读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限
  [DllImport("OUR_MIFARE.dll", EntryPoint = "pcdgetdevicenumber")]
  static extern byte pcdgetdevicenumber(byte[] devicenumber);//devicenumber用于返回编号


  //------------------------------------------------------------------------------------------------------------------------------------------------------
  //轻松读卡
  [DllImport("OUR_MIFARE.dll", EntryPoint = "piccreadex")]
  static extern byte piccreadex(byte ctrlword,byte[] serial,byte area,byte keyA1B0,byte[] picckey,byte[] piccdata0_2);
  //参数:说明
  //ctrlword:控制字
  //serial:卡序列号数组,用于指定或返回卡序列号
  //area:指定读卡区号
  //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。
  //picckey:指定卡密码,6个字节,卡出厂时的初始密码为6个0xff
  //piccdata0_2:用于返回卡该区第0块到第2块的数据,共48个字节.


  //------------------------------------------------------------------------------------------------------------------------------------------------------
  //轻松写卡
  [DllImport("OUR_MIFARE.dll", EntryPoint = "piccwriteex")]
  static extern byte piccwriteex(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] picckey, byte[] piccdata0_2);
  //参数:说明
  //ctrlword:控制字
  //serial:卡序列号数组,用于指定或返回卡序列号
  //area:指定读卡区号
  //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。
  //picckey:指定卡密码,6个字节,卡出厂时的初始密码为6个0xff
  //piccdata0_2:用于返回卡该区第0块到第2块的数据,共48个字节.


  //------------------------------------------------------------------------------------------------------------------------------------------------------
  //修改卡单区的密码
  [DllImport("OUR_MIFARE.dll", EntryPoint = "piccchangesinglekey")]
  static extern byte piccchangesinglekey(byte ctrlword, byte[] serial, byte area, byte keyA1B0, byte[] piccoldkey, byte[] piccnewkey);
  //参数:说明
  //ctrlword:控制字
  //serial:卡序列号数组,用于指定或返回卡序列号
  //area:指定读卡区号
  //keyA1B0:指定用A或B密码认证,一般是用A密码,只有特殊用途下才用B密码,在这不做详细解释。
  //piccoldkey://旧密码
  //piccnewkey://新密码.


  //------------------------------------------------------------------------------------------------------------------------------------------------------
  //发送显示内容到读卡器
  [DllImport("OUR_MIFARE.dll", EntryPoint = "lcddispfull")]
  static extern byte lcddispfull(string lcdstr);
  //参数:说明
  //lcdstr:显示内容
 
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button6 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(8, 16);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(104, 32);
   this.button1.TabIndex = 0;
   this.button1.Text = "轻松读卡";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(120, 16);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(104, 32);
   this.button2.TabIndex = 1;
   this.button2.Text = "轻松写卡";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(344, 16);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(104, 32);
   this.button3.TabIndex = 3;
   this.button3.Text = "让设备发出声响";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(232, 16);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(104, 32);
   this.button4.TabIndex = 2;
   this.button4.Text = "改单区密码";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(8, 64);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(440, 32);
   this.button5.TabIndex = 4;
   this.button5.Text = "读出设备全球唯一的设备编号,作为加密狗用";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 112);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(432, 24);
   this.label1.TabIndex = 5;
   this.label1.Text = "建议将OUR_MIFARE.dll和ICUSB.DLL复制到应用程序同一目录下";
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(8, 152);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(264, 21);
   this.textBox1.TabIndex = 6;
   this.textBox1.Text = "天下风云出我辈 一入江湖岁月催";
   //
   // button6
   //
   this.button6.Location = new System.Drawing.Point(288, 144);
   this.button6.Name = "button6";
   this.button6.Size = new System.Drawing.Size(160, 32);
   this.button6.TabIndex = 7;
   this.button6.Text = "发送字符至读卡器显示";
   this.button6.Click += new System.EventHandler(this.button6_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(456, 198);
   this.Controls.Add(this.button6);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }