求助C#调用32位动态库函数 制作明华读写器读写操作

lgmzyy 2011-03-21 11:56:49
 //寻卡
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_card")]
public static extern Int16 rf_card(int icdev, char _Mode, Int16* _Snr);

//将密码装入读写模块RAM中
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_load_key")]
public static extern int rf_load_key(int icdev, char _Mode, char _SecNr, Byte* _NKey);

//将密码装入读写模块RAM中
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_load_key_hex", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int rf_load_key_hex(int icdev, char _Mode, char _SecNr, Byte* _NKey);

上面是我写的调用dll函数。
    byte[] tk = new byte[6] { 255, 255, 255, 255, 255, 255 };
fixed (byte* array = tk)


if ((IC.rf_load_key_hex(icdev, '0', '1', array)) != 0)
{
MessageBox.Show("Load key error!!");
IC.Close_USB(icdev); //关闭设备
return;
}

这段是装载密码的函数 但是返回值一直是错误的,怎么都不返回0,请问各位大大 ,到底这个该怎么写啊
...全文
542 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyylgm 2011-03-22
  • 打赏
  • 举报
回复
恩 谢谢大哥 我自己努力研究研究
kernelkey 2011-03-22
  • 打赏
  • 举报
回复
<a href="http://www.17u.cn/FlightQuery.aspx?TCAllianceID=4788239&RefId=4788239&">不错啊</a>
lizhibin11 2011-03-22
  • 打赏
  • 举报
回复
那么多函数,不能每个都问,你自己看看文档不停的测试吧,这也是编程的乐趣所在啊。
zyylgm 2011-03-22
  • 打赏
  • 举报
回复
a0a1a2a3a4a5 是代表ffffffffffff 吗?
zyylgm 2011-03-22
  • 打赏
  • 举报
回复
我太菜了 现在
  int checkIC_pwd = IC.rf_authentication(icdev, 0, 1);
if (checkIC_pwd != 0)
{
MessageBox.Show("IC卡密码错误!");
IC.Close_USB(icdev); //关闭设备
return;
}

这个一直返回个4
 [DllImport("mwhrf_bj.dll", EntryPoint = "rf_authentication")]
public static extern int rf_authentication(int icdev, int mode, int SecNr);

怎么解决呢 ?
lizhibin11 2011-03-22
  • 打赏
  • 举报
回复
Byte[] 本身传的是一个地址,string,int等类型的参数需要被写入时才加ref out
lizhibin11 2011-03-22
  • 打赏
  • 举报
回复
ref应该是没必要的,编码应该是asc的。你自己试一下。
zyylgm 2011-03-22
  • 打赏
  • 举报
回复
  //读取数据
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_read")]
public static extern Int16 rf_read(int icdev, int Adr, ref Byte[] Data);

 Byte[] str = new Byte[16];
string strrr = "";
int read = -1;
read = IC.rf_read(icdev, 2,ref str);
strrr = Encoding.Default.GetString(str);
if (read == 0)
MessageBox.Show("IC卡中数据读取成功!" + strrr.ToString());
else
MessageBox.Show("IC卡中数据读取失败!" + strrr.ToString());

大哥我这样读数据 写的对吗?
lizhibin11 2011-03-22
  • 打赏
  • 举报
回复
发了,包括说明文档在内
zyylgm 2011-03-22
  • 打赏
  • 举报
回复

//读取数据
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_read")]
public static extern int rf_read(int icdev, int _Adr, ref Byte[] _Data);

读取数据 这样对吗?
顺便大哥可以把mwrf32.dll 发我邮箱一下吗?lgmzyy@163.com
zyylgm 2011-03-22
  • 打赏
  • 举报
回复
感谢大哥 返回正确
lizhibin11 2011-03-22
  • 打赏
  • 举报
回复
改成这样

[DllImport("mwrf32.dll", EntryPoint = "rf_load_key_hex")]
public static extern Int16 rf_load_key_hex(int icdev, int mode, int secnr, string keybuff);
int ints=IC.rf_load_key_hex(icdev, 0, 1, "a0a1a2a3a4a5");
zyylgm 2011-03-22
  • 打赏
  • 举报
回复
 byte[] tk = new byte[6] { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 };               
int ints=IC.rf_load_key_hex(icdev, 0, 1, tk);
if (ints != 0)
{
MessageBox.Show("Load key error!!");
}



大哥 返回值不对啊 不为0
lizhibin11 2011-03-22
  • 打赏
  • 举报
回复

[DllImport("mwrf32.dll", EntryPoint = "rf_load_key_hex", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public static extern Int16 rf_load_key_hex(int icdev, int mode, int secnr, [MarshalAs(UnmanagedType.LPArray)]byte[] keybuff);
zyylgm 2011-03-22
  • 打赏
  • 举报
回复
感谢各位大哥,我终于写好了!下面贡献我的代码有需要的朋友 请查阅,代码不严谨,有错误请指出,谢谢
 //连接设备
[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_get_status")]
public static extern int rf_get_status(int icdev, Byte[] version);
//蜂鸣器
[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_halt")]
public static extern int rf_halt(Int16 icdev);

//寻卡请求
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_request" , SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern Int16 rf_request(int icdev,string IDLE, Int16* state);


//寻卡
[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_anticoll", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int rf_anticoll(int icdev, char _Bcnt, Int64* _Snr);

////将密码装入读写模块RAM中

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

[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);

//验证某一扇区密码 2
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_authentication_2")]
public static extern int rf_authentication_2(int icdev, int _Mode, int KeyNr, int Adr);


//向卡中写入数据
[DllImport("mwhrf_bj.dll", EntryPoint = "rf_write")]
public static extern int rf_write(int icdev, int _Adr, char[] _Data);

[DllImport("mwhrf_bj.dll")]
public static extern short rf_write(int icdev, int adr, [In] string sdata); //向卡中写入数据

//读取数据
[DllImport("mwhrf_bj.dll")]
public static extern short rf_read(int icdev, int adr, [MarshalAs(UnmanagedType.LPStr)] StringBuilder sdata); //从卡中读数据
lgmzyy 2011-03-21
  • 打赏
  • 举报
回复
int rf_load_key(int icdev,unsigned char _Mode,unsigned char _SecNr,unsigned char *_NKey);

功 能:将密码装入读写模块RAM中

参 数:icdev:通讯设备标识符

_Mode:装入密码模式,同密码验证模式mode_auth

_SecNr:扇区号(M1卡:0~15; ML卡:0)

_Nkey:写入读写器中的卡密码

返 回:成功则返回 0

例://key A and key B

unsigned char tk[6]={0xa0,0xa1,0xa2,0xa3,0xa4,0xa5};

/* 装入1扇区的0套A密码 */

if((rf_load_key(icdev,0,1,tk))!=0)

{

printf("Load key error!");

rf_exit(icdev)

exit(1);

}




这个是对rf_load_key的实例代码
lgmzyy 2011-03-21
  • 打赏
  • 举报
回复
  byte[] tk = new byte[6] { 255, 255, 255, 255, 255, 255 };
fixed (byte* array = tk)
if ((IC.rf_load_key(icdev, '0', '1', array)) != 0)
{
MessageBox.Show("Load key error!!");
}

用rf_load_key 这个函数也一直是错误的,不返回0

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧