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中方法说明以及事例:
功 能:将密码装入读写模块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);       }

解决方案 »

  1.   

    public extern static int rf_load_key(int icdev, byte _Mode, byte _SecNr, ref StringBuilder _NKey);
      

  2.   

    能告诉我那个实例用C#怎么写么? 用ref还是不行~!··
      

  3.   

    StringBuilder _NKey=new StringBuilder ();
    _NKey.append("a0")
    .......
      

  4.   


    StringBuilder pwd = new StringBuilder();
                    pwd.Append(0xa0);
                    pwd.Append(0xa1);
                    pwd.Append(0xa2);
                    pwd.Append(0xa3);
                    pwd.Append(0xa4);
                    pwd.Append(0xa5);
    int a = general.rf_load_key(rHandle.ToInt32(), '0', '1',ref pwd);
    上边这样还不行,求各位高手怎么做啊,我的返回值一直不是0 但是在不给pwd赋值的时候是正确的,可以返回0 但是我想传值进去把密码装载进去怎么做?
      

  5.   

    没用过,怀疑StringBuilder 能成功将值传给char *么?
      

  6.   

    是编码问题吗?试试 CharSet,改为其他值
      

  7.   

    例://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);       }是这个例子用C#怎么写????我调用这个方法总是调用不了,密码装载不进去啊!!!高手们帮忙啊
      

  8.   

    刚好我也在做IC卡的项目可以帮到你,哈哈!
    直接用STRING就可以了,你的读写密码不用那么写直接STRING TK=‘FFFFFFFFFFFF’就可以了!前面两个参数应该用INT类型的吧!希望对你有帮助
      

  9.   

    不行啊,我的密码还是装载不进去,string  StringBuilder 能试的都试过了,没人做过这个么?
    icdev:通讯设备标识符       _Mode:装入密码模式,同密码验证模式mode_auth       _SecNr:扇区号(M1卡:0~15;  ML卡:0)       _Nkey:写入读写器中的卡密码
    参数第一个int  第二个和第三个是char 类型 第四个怎么弄不清楚如果第四个不给赋值就可以装载,一旦有值就装载不上,高手们帮忙!~
    10楼的方法也不行啊
      

  10.   

    参数第一个int 第二个和第三个应该是INT 类型 第四个参数是STRING就行了,你装载不了还要确定你的读写密码是否正确!
      

  11.   


    我的参数int rf_load_key(int icdev,unsigned char _Mode,unsigned char _SecNr,unsigned char *_NKey);
    确实是char不是int 这个是里边的函数, 密码是否正确?我这个只是装载密码,是6个16进制就可以了,并不是核对卡密,核对卡密是另外一个函数
      

  12.   

    int rf_load_key(HANDLE icdev,unsigned char _Mode,unsigned char _SecNr,unsigned 
    char *_NKey); 
    我的DLL也是这样定义的,但是我C#定义二,三个参数也是用INT的,最后一个参数用STRING就不用十六进行了,直接用字符就可以了!
      

  13.   


    不对,前边的参数应该没有影响,我也改成了int   
    string str = "FFFFFFFFFFFF";
    int a = general.rf_load_key(rHandle.ToInt32(), 0, 1, str);
    这个是我的调用,但是返回值a还是不等于0 我不知道你在做哪方面的但是我这个这么写是不正确的
    StringBuilder sss = new StringBuilder();
                    int a = general.rf_load_key(rHandle.ToInt32(), '0', '1', sss);
    如果这么调用的话,返回值是0 也就是装载成功了,没有值或者是不存在的。
    就上面的方法,如果使用int类型,同样不会成功。
      

  14.   

    你看看有没有rf_load_key_hex这个函数,如果有,用我上面说有就可以成功了!
      

  15.   


    int a = general.rf_load_key_hex(rHandle.ToInt32(), 0, 1, "ffffffffffff");
    真可惜,我这么调用还是没有成功
    int a = general.rf_load_key_hex(rHandle.ToInt32(), '0', '1', "ffffffffffff");
    这种也试验了,还是不成功!~
    郁闷死了,给我的事例程序是用PB语言写的。
      

  16.   

    那还真帮不了你了,我就是rf_load_key_hex(rHandle.ToInt32(), 0, 1, "ffffffffffff");
    这样调用的!我的完全没有问题!
      

  17.   

    你的返回值是0???为什么我的总是不成功?打开端口
    rHandle = general.rf_init(Iport, baud);
    general.rf_card(rHandle.ToInt32(), '1', value); 寻卡
    int a = general.rf_load_key_hex(rHandle.ToInt32(), '0', '1', "ffffffffffff");
    前边2步都可以成功,也可以找到卡片,但是一到装载的地方就不能用,为什么啊你的是怎么写的?
      

  18.   

    我的是返回0的,你rf_card(rHandle.ToInt32(), '0', value); 试试
      

  19.   

    public extern static int rf_load_key(int icdev, byte _Mode, byte _SecNr, byte[] _NKey);
      

  20.   

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

  21.   

    还有一个问题就是我的返回的卡号并不是标准的卡号例如0527289660 这个卡号是使用设备提供商提供的程序读取出来的,但是使用我自己写的程序独取出来的就是527289660 这样了,还有-085123026 读取出来就是-85123206 不知道是为什么
    IntPtr value=Marshal.AllocHGlobal(1024);
    general.rf_card(rHandle.ToInt32(), '0', value);
    label3.Text = Marshal.ReadInt32(value).ToString();
    你是如何写的?
      

  22.   

    handle---------IntPtr
    hwnd-----------IntPtr
    char *----------string
    int * -----------ref int
    int &-----------ref int
    void *----------IntPtr
    unsigned char *-----ref byte
    Struct需要在C#里重新定义一个Structint __stdcall FunctionName(unsigned char ¶m1, unsigned char *param2) 在C#中对其进行调用的方法是:[DllImport(“ file ”)]  extern static int FunctionName(ref byte param1, ref byte param2)可以解决你的问题了
      

  23.   

    真郁闷,我的问题还是没有解决,现在我都怀疑是我前边写错了,现在我给我我所有的C#和DLL里边的函数代码[DllImport("mwrf32.dll", EntryPoint = "rf_card", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public extern static int rf_card(int icdev,byte _Mode,ref uint _Snr);
    [DllImport("mwrf32.dll", EntryPoint = "rf_init", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public extern static IntPtr  rf_init(int port,long baud);
    [DllImport("mwrf32.dll", EntryPoint = "rf_load_key", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public extern static int rf_load_key(int icdev, byte _Mode, byte _SecNr, ref byte _NKey);
    [DllImport("mwrf32.dll", EntryPoint = "rf_load_key_hex", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public extern static int rf_load_key_hex(int icdev, char _Mode, char _SecNr, StringBuilder _NKey);
    [DllImport("mwrf32.dll",EntryPoint="rf_beep", SetLastError=true,ExactSpelling=true,CallingConvention=CallingConvention.StdCall, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public extern static int rf_beep(int icdev, uint Msec);上边的是我调用DLL 里边的函数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.ToInt32(), 10);
            }
    //在页面初始化的同时初始化串口连接,并且蜂鸣一次
    uint value = new uint();
    int bbb=general.rf_card(rHandle.ToInt32(), 0x00, ref value);/*寻卡(这里可以找到卡,但是找到卡后返回值不是0而是一组看上去无意义的数字而且还存在我上边说的问题,还有一个问题就是我的返回的卡号并不是标准的卡号例如0527289660 这个卡号是使用设备提供商提供的程序读取出来的,但是使用我自己写的程序独取出来的就是527289660 这样了,还有-085123026 读取出来就是-85123206 不知道是为什么 */
    //这里是寻卡的问题
    byte[] pwd = new byte[] {0x42,0x4c,0x41,0x4e,0x4b,0x4b};
    int a = general.rf_load_key(rHandle.ToInt32(), 0, 1,ref pwd[0]);
    //将密码装载到设备中,到这里就总是出错,无论如何都不返回0下边附上DLL中函数事例int  rf_init(int port,long baud);功 能:初始化串口参 数:port:串口号,取值为0~3       baud:为通讯波特率9600~115200返 回:成功则返回串口标识符>0,失败返回负值,见错误代码表   例:int icdev;       icdev=rf_init(0,9600);//初始化串口1,波特率9600 如果是WIN32程序则icdev为设备句柄,见下例:       HANDLE icdev;       icdev=rf_init(0,115200);       if(icdev<0)              MessageBox(“rf_init error”);int rf_card(int icdev,unsigned char _Mode,unsigned long *_Snr);功 能:寻卡,能返回在工作区域内某张卡的序列号参 数:icdev:通讯设备标识符       _Mode:寻卡模式mode_card       _Snr:返回的卡序列号返 回:成功则返回 0   例:int st;       unsigned long snr;       st=rf_card(icdev,0,&snr);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);       }
    int rf_beep(int icdev,unsigned int _Msec);功 能:蜂鸣参 数:icdev:通讯设备标识符       unsigned int _Msec:蜂鸣时间,单位是10毫秒返 回:成功则返回 0   例:int st;       st=rf_beep(icdev,10);             /*鸣叫100毫秒*/
      

  24.   

    你试试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