xinyaping(眼镜哥)及各位大牛看过来 有关P-invoke

烟波钓 2014-01-02 03:33:03
拿到一个C++的动态链接库
有个方法需要传结构体,但是结构体里面又有函数指针


如下:

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

一直也解决不了 快崩溃了
...全文
227 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
高崇进 2016-06-27
  • 打赏
  • 举报
回复
大神,我也碰到相同的问题。如果问题解决了可以告诉我吗? qq632701221
oYouDian12 2015-07-09
  • 打赏
  • 举报
回复
眼镜哥 ! 我也在弄宝莱特监护仪 也碰到相同的问题了, 不知道你解决没? 如果解决麻烦告诉我。 qq 25921877
烟波钓 2014-01-02
  • 打赏
  • 举报
回复
与线程无关,实验过了 纠结了
烟波钓 2014-01-02
  • 打赏
  • 举报
回复
引用 2 楼 yanbuodiao 的回复:
[quote=引用 1 楼 gomoku 的回复:] 一般来说c++的long对应c#的int: public extern static int InitHL7SDK(ref SDKStruct pHL7SDKInterace, int serverMode, int serverIP, int localIP);
谢谢 修改完成后,还是没有效果 调用是这样

 Console.WriteLine("serverIP:" + args[0] + ", localIP:" + args[1] + ", mode:" + args[2]);
                var HL7SDK = new HL7SDK_Interface();

                var status = HL7SDK.loadSDK(args[0], args[1], int.Parse(args[2]));
                Console.WriteLine("loadSDK:" + status);
                Console.ReadLine();
是不是会阻塞? 我先起一个线程试试[/quote] 我先起一个线程试试
烟波钓 2014-01-02
  • 打赏
  • 举报
回复
引用 1 楼 gomoku 的回复:
一般来说c++的long对应c#的int: public extern static int InitHL7SDK(ref SDKStruct pHL7SDKInterace, int serverMode, int serverIP, int localIP);
谢谢 修改完成后,还是没有效果 调用是这样

 Console.WriteLine("serverIP:" + args[0] + ", localIP:" + args[1] + ", mode:" + args[2]);
                var HL7SDK = new HL7SDK_Interface();

                var status = HL7SDK.loadSDK(args[0], args[1], int.Parse(args[2]));
                Console.WriteLine("loadSDK:" + status);
                Console.ReadLine();
是不是会阻塞? 我先起一个现场试试
gomoku 2014-01-02
  • 打赏
  • 举报
回复
一般来说c++的long对应c#的int: public extern static int InitHL7SDK(ref SDKStruct pHL7SDKInterace, int serverMode, int serverIP, int localIP);

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧