DllImport 回调函数最后一次调用报“程序错误”
---------- C++ -----------------
HHNET_API HHERR_CODE WINAPI HHNET_SearchAllServer(UINT nTimeWait,
void(WINAPI *CallbackServerFind)(DVS_MACHINE_TYPE nDeviceType,char *pDeviceName,
char *pIP,BYTE macAddr[6],WORD wPortWeb,WORD wPortListen,char *pSubMask,
char *pGateway,char *pMultiAddr,char *pDnsAddr,WORD wMultiPort,int nChannelNum,int nFindCount,DWORD dwDeviceID));
------------- C# ------------------
[DllImport("HHNetClient.dll")]
private static extern int HHNET_SearchAllServer(UInt32 nTimeWait,CallbackServerFind fCallbackServerFind);
public delegate void CallbackServerFind(DVS_MACHINE_TYPE nDeviceType, string pDeviceName, string pIP, byte[] macAddr, ushort wPortWeb, ushort wPortListen, string pSubMask, string pGateway, string pMultiAddr, string pDnsAddr, ushort wMultiPort, int nChannelNum, int nFindCount);
public static CallbackServerFind fCallbackServerFind;
public long GetDeviceInfo()
{
fCallbackServerFind = new CallbackServerFind(m_vodCallbackServerFind);
res = HHNET_SearchAllServer(2345, fCallbackServerFind);
Thread.Sleep(1000);
}
public void m_vodCallbackServerFind(DVS_MACHINE_TYPE nDeviceType, string pDeviceName, string pIP, byte[] macAddr , ushort wPortWeb, ushort wPortListen, string pSubMask, string pGateway, string pMultiAddr, string pDnsAddr, ushort wMultiPort, int nChannelNum, int nFindCount)
{
dName[Num] = pDeviceName;
Num = Num + 1;
return;
}
HHNET_SearchAllServer会多次调用Callback且Callback能调用,可是调用到最后一次时报“..程序错误..关闭”
------解决方案--------------------加上调用约定,c++默认为Cdecl
[DllImport("HHNetClient.dll"),CallingConvention.Cdecl]
------解决方案--------------------原函数上标了 WINAPI ,加 stdcall 还差不多
原函数的回调函数里最后还有个参数 DWORD dwDeviceID,你 C# 定义的 delegate 少了那个参数
void(WINAPI *CallbackServerFind)(DVS_MACHINE_TYPE nDeviceType,char *pDeviceName,
char *pIP,BYTE macAddr[6],WORD wPortWeb,WORD wPortListen,char *pSubMask,
char *pGateway,char *pMultiAddr,char *pDnsAddr,WORD wMultiPort,int nChannelNum,int nFindCount,DWORD dwDeviceID));