明华USBIC卡读卡器读写程序实例
2018-12-02 本文已影响2人
05b0963bb480
目前,市面上很多的会员卡或者储值卡都是使用了明华USB接口读卡器制作的,在初始化卡信息时,就需要用调用到mwhrf_bj.dll。今天为大家分享,C#下如何调用mwhrf_bj.dll实现卡数据的读写。
C#下调用C++的DLL,需要使用 DLLImport导入:
实现接口
[DllImport("mwhrf_bj.dll", EntryPoint = "Open_USB")]
public static extern int Open_USB();
[DllImport("mwhrf_bj.dll", EntryPoint = "Close_USB", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int Close_USB(int icdev);
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_beep", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern int rf_beep(int icdev, int version);
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_load_key_hex")]
public static extern Int16 rf_load_key_hex(int icdev, int mode, int secnr, string keybuff);
[DllImport("mwhrf_bj.dll")]
public static extern short rf_authentication(int icdev, int _Mode, int _SecNr);
[DllImport("mwhrf_bj.dll")]
public static extern short rf_read(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata);
[DllImport("mwhrf_bj.dll")]
public static extern short rf_write(int icdev, int adr, [In] string sdata);
//寻卡
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_card")]
public static extern Int16 rf_card(int icdev, int mode, ref int Snr);
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_load_key", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
//说明: 返回设备当前状态
public static extern Int16 rf_load_key(int icdev, int mode, int secnr, [MarshalAs(UnmanagedType.LPArray)]byte[] keybuff);
public int InitIC()
{
return Open_USB();
}
public int Exit(int icdev)
{
return Close_USB(icdev);
}
public int Beep(int icdev, int msec)
{
return rf_beep(icdev, msec);
}
public int load_key(int icdev, int adr, byte[] sdata)
{
return rf_load_key(icdev, 0, adr, sdata);
}
public int Authentication(int icdev, int model, int adr)
{
return rf_authentication(icdev, 0, adr);
}
public int Read(int icdev, int adr, ref StringBuilder sdata)
{
StringBuilder sb = new StringBuilder();
int st = rf_read(icdev, adr, sb);
string str = sb.ToString();
int i = str.IndexOf('A');
string sbstr = "";
if (i > 0)
{
sbstr = str.Substring(0, i);
}
else
{
sbstr = str;
}
StringBuilder sbtmp = new StringBuilder(sbstr);
sdata = sbtmp;
//sdata = sb;
return st;
}
public int Write(int icdev, int adr, string sdata)
{
return rf_write(icdev, adr, sdata);
}
public int IcCard(int icdev, ref int snr)
{
return rf_card(icdev, 1, ref snr);
}
IC接口
/// 初始化
int InitIC();
/// 断开
int Exit(int icdev);
/// 蜂鸣
int Beep(int icdev, int msec);
/// 装载密码
int load_key(int icdev, int adr, byte[] keybuff);
/// 验证密码 返回0为正确
int Authentication(int icdev, int model, int adr);
/// 读卡
int Read(int icdev, int adr,ref StringBuilder sdata);
/// 写卡
int Write(int icdev, int adr, string sdata);
/// 寻卡
int IcCard(int icdev, ref int snr);
调用实例
读卡代码
icdev = ic.InitIC();
int snd = -1;
int st3 = ic.IcCard(icdev, ref snd);
int st = 0;
ic.load_key((Int16)icdev, addr, IcPwd());
st = ic.Authentication((Int16)icdev, 0, addr);
if (st != 0)
{
MessageBox.Show("IC卡无效!");
ic.Exit(icdev);
return;
}
StringBuilder one = new StringBuilder();
ic.Read(icdev, addr * 4, ref one);
StringBuilder two = new StringBuilder();
ic.Read(icdev, addr * 4 + 1, ref two);
StringBuilder three = new StringBuilder();
ic.Read(icdev, addr * 4 + 2, ref three);
((TextBox)this.Controls.Find(string.Format("txt{0}Addr1", addr), true)[0]).Text = one.ToString();
((TextBox)this.Controls.Find(string.Format("txt{0}Addr2", addr), true)[0]).Text = two.ToString();
((TextBox)this.Controls.Find(string.Format("txt{0}Addr3", addr), true)[0]).Text = three.ToString();
ic.Exit(icdev);
写卡代码
int iResult1 = 0, iResult2 = 0, iResult3 = 0;
icdev = ic.InitIC();
int snd = -1;
int st3 = ic.IcCard(icdev, ref snd);
int st = 0;
ic.load_key((Int16)icdev, addr, IcPwd());
st = ic.Authentication((Int16)icdev, 0, addr);
if (st != 0)
{
MessageBox.Show("IC卡无效!");
ic.Exit(icdev);
return;
}
byte[] buff = new byte[32];
byte[] databuff = new byte[16];
string data = this.txtNewPass.Text.Trim() + "FF078069FFFFFFFFFFFF";
buff = Encoding.ASCII.GetBytes(data);
int kadr = addr * 4 + 3;
common.a_hex(buff, databuff, 32);
st = (int)mifareone.rf_write_hex(this.icdev, kadr, buff);
string one = "", two = "", three = "";
one = ((TextBox)this.Controls.Find(string.Format("txt{0}Addr1", addr), true)[0]).Text.Trim();
two = ((TextBox)this.Controls.Find(string.Format("txt{0}Addr2", addr), true)[0]).Text.Trim();
three = ((TextBox)this.Controls.Find(string.Format("txt{0}Addr3", addr), true)[0]).Text.Trim();
iResult1 = ic.Write(this.icdev, addr * 4, one);
iResult2 = ic.Write(this.icdev, addr * 4 + 1, two);
iResult3 = ic.Write(this.icdev, addr * 4 + 2, three);
if (iResult1 == 0 && iResult2 == 0 && iResult3 == 0)
{
ic.Beep(this.icdev, 20);
}
ic.Exit(icdev);