这个程序是用来读取IC里面存储的信息的,在C++ 测试程序中可以正常使用,但是C#程序里面无法使用,输入的指令没有错误。我的程序错在哪里呢,请各位大师帮忙看看。被调用C++ DLL中函数部分源码 :extern "C" __declspec(dllexport)   int __stdcall IccIsoCommand(int slot,APDU_SEND  *ApduSend,APDU_RESP  *ApduRecv, unsigned char *DataBuf, unsigned char *Response, DWORD size);
typedef struct 
{
unsigned char Command[4];     // 命令头
unsigned int Lc; // 要发送的数据长度
unsigned int Le; // 要接收的数据长度
} APDU_SEND;
typedef struct
{
unsigned int LenOut; //输出数据长度
unsigned char SWA; //响应尾
unsigned char SWB; //
} APDU_RESP;
我写的C# 程序调用代码:[DllImport("SmartCard.dll", EntryPoint = "IccIsoCommand")]public static extern int IccIsoCommand(int slot, ref APDU_SEND ApduSend, ref APDU_RESP ApduRecv, byte[] buffer , ref string Response, uint size);        [StructLayout(LayoutKind.Sequential/*,Pack = 1*/)]
        public struct APDU_SEND
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
            public byte[] Command;
            public uint Lc;
            public uint Le;
        }
        [StructLayout(LayoutKind.Sequential/*,Pack = 1*/)]
        public struct APDU_RESP
        {
            public uint LenOut;
            public byte SWA;
            public byte SWB;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            uint size = 0;
            int result =0;
            int slot = 2;
            APDU_SEND aps = new APDU_SEND();  
            APDU_RESP apr = new APDU_RESP();  
            string res = "";
            byte[] buffer = new byte[500];            //aps.Command = new byte[] { 0x00, 0xB0, 0x82, 0x00 };  
            //aps.Lc = 0x00;
            //aps.Le = 0x5F;
            
            aps.Command = new byte[] {0x00,0xa4,0x00,0x00 };  //赋指令
            aps.Lc = 0x02;
            aps.Le = 0x00;
            buffer[0] = 0x3f; // 这条命令需要buffer数据,执行时候程序直接崩溃。有的指令不需要buffer数据,可
                              // 以执行但是得不到正确结果
            buffer[1] = 0x00;            MessageBox.Show("开始执行IccIsoCommand函数!");            result = IccIsoCommand(slot, ref aps, ref  apr, buffer, ref  res, size);  //看到这里就可以了。我需要从buffer 或者res里面读出数据。但是无法读出,在返回的结构体里面也没有测试 SWA、SWB也不正确。             

解决方案 »

  1.   

    http://baike.baidu.com/view/1276580.htm?fr=ala0_1_1(调用函数的,去看看咯,应该有点帮助)
      

  2.   

    函数声明有问题:
    [DllImport("SmartCard.dll", EntryPoint = "IccIsoCommand",CallingConvention =CallingConvention.Stdcall)]
    public static extern int IccIsoCommand(int slot, ref APDU_SEND ApduSend, ref APDU_RESP ApduRecv, [In,Out]byte[] buffer , ref string Response, uint size);
      

  3.   

    这个好像以前有人问过这个函数,你在csdn里找找吧。
    另外C++里的Unsign int 在C#里使用int就行。Uint大了。
    public static extern int IccIsoCommand(int slot, APDU_SEND ApduSend, ref APDU_RESP ApduRecv, ref byte[] buffer , StringBuilder Response, int size);
      

  4.   

    http://topic.csdn.net/u/20110707/11/E68D58A2-F858-4702-8528-CAFF32D72EAA.html
      

  5.   

    APDU_SEND,这个好像我之前的公司写过这样的api