现在要做的事情是需要用c#调用c++写的dll,当dll(dll怎样跟硬件通讯的不管)读取到卡片信息时,通过回调函数把卡片信息传递到C#这边来
c# 这边调用此方法设置回调函数:
[DllImport("TCP102_SDK.dll", EntryPoint = "?TCP102_InitCallBack@@YGEKPAU_CALLBACK_FUN@@@Z")]
public unsafe static extern byte TCP105_InitCallBack(uint dwUser, ref CALLBACK_FUN p_call_back);troller@@YGEPADEE@Z")]
回调函数:
//STRUCT
[StructLayout(LayoutKind.Sequential)]
public struct CARD_NUMBER_MSG
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] ControllerIP;
public uint CommandSeq;
public uint ReaderID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] CardID;
public string print()
{
return "CARD_NUMBER_MSG information : ControllerIP:" + String.Format("{0}.{1}.{2}.{3}", ControllerIP[0], ControllerIP[1], ControllerIP[2], ControllerIP[3]) + " CommandSeq:" + CommandSeq + " ReaderID: " + ReaderID + " CardID " + String.Format("{0}|长度:{1}", CardID[0], CardID.Length);
}
}
public delegate void ReadedCardNumber(uint dwUser, ref CARD_NUMBER_M
SG p_card_msg);
[StructLayout(LayoutKind.Sequential)]
public struct CALLBACK_FUN
{
public ReadedCardNumber p_ReadedCardNumberCallBack;
}
c++里面的设置回调函数的代码:
/******************************************************************************************************
* Function Name : TCP102SetReceivedDataCallBack
* Description : 设置回调
* Arguments : None.
* Output : None.
* Return : None.
* Note(s) : None.
*******************************************************************************************************/
TCP102_SDK_API unsigned char __stdcall TCP102_InitCallBack(DWORD dwUser,CALLBACK_FUN *p_call_back)
{
if((NULL==p_call_back)||(0==dwUser))
{
return TCP102_ERR_PARAMETER;
}
//设置回调函数指针
ReadedCardNumberCallBack = p_call_back->p_ReadedCardNumberCallBack;
h_CallBackWindow = dwUser;
return TCP102_ERR_OK;
}
要实现的功能就是正确的初始化、设置回调函数...后,当sdk有读到卡片数据后,将读到的数据通过回调函数传递到c#这边来。
现在的问题是,第一次触发dll把读到的卡片信息回调到c#这边可以正确执行。但第二次就报未实例化对象的错误了。想知道有大神遇到过这样的问题吗?求解答。(目前调试的结果是还没触发回调方法就报未实例化对象的错了,在想是不是没找到回调方法的指针地址.)