日期:2014-05-18  浏览次数:20741 次

求助C#调用32位动态库函数
int rf_load_key(int icdev,unsigned char _Mode,unsigned char _SecNr,unsigned char *_NKey);
这个方法是DLL中的方法,引入DLL后
 public extern static int rf_load_key(int icdev, char _Mode, char _SecNr, StringBuilder _NKey);
在C#中这么写这个方法。
但是最后一个参数是传递过去,将数据装载到我的IC刷卡设备中,总是装载不上,是什么原因。我用的应该是明华的DLL

另付DLL中方法说明以及事例:
C/C++ code

功 能:将密码装入读写模块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);

       }




------解决方案--------------------
刚好我也在做IC卡的项目可以帮到你,哈哈!
直接用STRING就可以了,你的读写密码不用那么写直接STRING TK=‘FFFFFFFFFFFF’就可以了!前面两个参数应该用INT类型的吧!希望对你有帮助
------解决方案--------------------
你看看有没有rf_load_key_hex这个函数,如果有,用我上面说有就可以成功了!
------解决方案--------------------
那还真帮不了你了,我就是rf_load_key_hex(rHandle.ToInt32(), 0, 1, "ffffffffffff");
这样调用的!我的完全没有问题!
------解决方案--------------------
我的是返回0的,你rf_card(rHandle.ToInt32(), '0', value); 试试
------解决方案--------------------
public extern static int rf_load_key(int icdev, byte _Mode, byte _SecNr, ref byte _NKey);

byte[] _NKey=new byte[]{0xa0,0xa1,0xa2,0xa3,0xa4,0xa5};


rf_load_key(rHandle.ToInt32(), 0, 1, ref _NKey[0]);

------解决方案--------------------
handle---------IntPtr
hwnd-----------IntPtr
char *----------string
int * -----------ref int
int &-----------ref int
void *----------IntPtr
unsigned char *-----ref byte
Struct需要在C#里重新定义一个Struct

int __stdcall FunctionName(unsigned char ¶m1, unsigned char *param2) 在C#中对其进行调用的方法是:

[DllImport(“ file ”)] extern static int FunctionName(ref byte param1, ref byte param2)

可以解决你的问题了
------解决方案--------------------
你试试DLL定义:
[DllImport("mwrf32.dll")]
public static extern IntPtr rf_init(int port, int baud);
[DllImport("mwrf32.dll")]
public static extern int rf_exit(IntPtr icdev);
[DllImport("mwrf32.dll")]
public static extern int rf_card(IntPtr icdev, int _Mode, ref ulong _Snr);
[DllImport("mwrf32.dll")]
public static extern int rf_beep(IntPtr icdev, int _Msec);
[DllImport("mwrf32.dll")]
public static extern int rf_load_key_hex(IntPtr icdev, int _Mode, int _SecNr, string _NKey);

读卡:
static IntPtr rHandle = IntPtr.Zero;
public IC_initialize(int Iport, long baud)
{
InitializeComponent();
this.MaximizeBox = false;
rHandle = general.rf_init(Iport, baud);
EquipmentOperation.rf_beep(rHandle, 10);
}
//在页面初始化的同时初始化串口连接,并且蜂鸣一次
ulong value = 0;
int bbb=general.rf_card(rHandle, 0, ref value);/*寻卡(这里可以找到卡,但是找到卡后返回值不是0而是一组看上去无意义的数字而且还存在我上边说的问题,还有一个问题就是我的返回的卡号并不是标准的卡号例如0527289660 这个卡号是使用设备提供商提供的程序读取出来的,但是使用我自己写的程序独取出来的就是527289660 这样了,还有-085123026 读取出来就是-85123206 不知道是为什么 */
//这里是寻卡的问题
string pwd = "ffffffffffff";
int a = general.rf_load_key_hex(rHandle, 0, 1,pwd);
//将密码装载到设备中,到这里就总是出错,无论如何都不返回0