这个程序是用来读取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也不正确。
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也不正确。
[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);
另外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);