有个方法需要传结构体,但是结构体里面又有函数指针
如下:
struct HL7SDK_INTERFACE
{
void (* OnConnect) (int iBedNO); //the TCP connection from one bed has built, data can be received
void (* OnDisconnect)(int iBedNO); //the TCP connection from one bed has disconnected, abnormal condition happened
void (* OnRecvHL7Msg)(int iBedNO, void * pMsg, int iLen, int iMsgType); //received original HL7 message of one bed(iBedNO), such as 0x0B......0x1C 0x0D
void (* OnSendHL7Msg)(int iBedNO, void *pMsg, int iLen, int iMsgType); //sent original HL7 message of one bed(iBedNO), such as 0x0B......0x1C 0x0D
};
方法调用如下:
nt InitHL7SDK(PHL7SDK_INTERFACE pHL7SDKInterace, enum HL7_WORK_MODE enHL7ServerMode, unsigned long ulServerIP, unsigned long ulLocalIP)
我在C#里面处理如下:
SDKStruct SDKIf;
public int loadSDK(string server, string local, int mode)
{
SDKIf = new SDKStruct
{
onConnect = onConnect,
onDisconnect = onDisconnect,
onRecvHL7Msg = onRecvHL7Msg,
onSendHL7Msg = onSendHL7Msg,
onRecvMonitorData = onRecvMonitorData
};
var serverIP = getIpValue(server);
var localIP = getIpValue(local);
var ret = CFunction.InitHL7SDK(ref SDKIf, mode, serverIP, localIP);
return ret;
}
public int freeSDK()
{
int ret = CFunction.ExitHL7SDK();
return ret;
}
public long getIpValue(string ip)
{
var strs = ip.Split('.');
var ipInt = new uint[4];
try
{
for (var i = 0; i < strs.Length; i++)
{
ipInt[i] = uint.Parse(strs[i]);
}
return ((ipInt[0] << 24) | (ipInt[1] << 16) | (ipInt[2] << 8) | ipInt[3]);
}
catch (Exception exp)
{
return 0;
}
}
public void onConnect(int iBedNo)
{
Console.WriteLine("connect: bedNo:" + iBedNo);
}
public void onDisconnect(int iBedNo)
{
Console.WriteLine("disconnect: bedNo:" + iBedNo);
}
public void onRecvHL7Msg(int iBedNo, IntPtr pMsg, int iLen, int iMsgType)
{
Console.WriteLine("recvHL7Msg: bedNo:" + iBedNo + ", size:" + iLen + ", type:" + iMsgType);
}
public void onSendHL7Msg(int iBedNo, IntPtr pMsg, int iLen, int iMsgType)
{
Console.WriteLine("onSendHL7Msg: bedNo:" + iBedNo + ", size:" + iLen + ", type:" + iMsgType);
}
public void onRecvMonitorData(int iBedNo, IntPtr pData, int iLen, int iDataType)
{
//IntPtr pPonitor = new IntPtr(pData.ToInt64());
Console.WriteLine("recvData: bedNo:" + iBedNo + ", size:" + iLen + ", type:" + iDataType);
}
}
[StructLayout(LayoutKind.Sequential)]
struct SDKStruct
{
[MarshalAs(UnmanagedType.FunctionPtr)]
public func_connect onConnect;
[MarshalAs(UnmanagedType.FunctionPtr)]
public func_disconnect onDisconnect;
[MarshalAs(UnmanagedType.FunctionPtr)]
public func_recvHL7Msg onRecvHL7Msg;
[MarshalAs(UnmanagedType.FunctionPtr)]
public func_sendHL7Msg onSendHL7Msg;
[MarshalAs(UnmanagedType.FunctionPtr)]
public func_recvMonitorData onRecvMonitorData;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void func_connect(int iBedNo);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void func_disconnect(int iBedNo);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void func_recvHL7Msg(int iBedNo, IntPtr pMsg, int iLen, int iMsgType);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void func_sendHL7Msg(int iBedNo, IntPtr pMsg, int iLen, int iMsgType);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void func_recvMonitorData(int iBedNo, IntPtr pData, int iLen, int iDataType);
}
class CFunction
{
[DllImport("HL7SDK.dll", EntryPoint = "InitHL7SDK", CallingConvention = CallingConvention.Cdecl)]
public extern static int InitHL7SDK(ref SDKStruct pHL7SDKInterace, long serverMode, long serverIP, long localIP);
[DllImport("HL7SDK.dll")]
public extern static int ExitHL7SDK();
[DllImport("HL7SDK.dll")]
public extern static int GetHL7ServerInfo(long serverInfo);
[DllImport("HL7SDK.dll")]
public extern static int UpdateCISData(int iBedNo, IntPtr pData, int iLen, int iDataType);
}
但是方法起来之后,这些穿进去的方法就一直没有执行成功,网上也有这个问题C# Pinvoke with C struct contains function pointer
一直也解决不了 快崩溃了