C++函数定义转C#问题
这是C++头文件的函数定义,那转成C#该是如何呢?特别是第二个参数,转了半天都有Error
C++
libmosq_EXPORT void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int));
C#(我自己转的,不对)
private static extern void mosquitto_message_callback_set(IntPtr mosq, IntPtr(on_message)(IntPtr mosquitto, IntPtr, IntPtr mosquitto_message));
求教大师
------解决方案--------------------函数指针,你应该定义成委托:
void delegate on_connectDelegate(mosquitto mosq, IntPtr p, int i);
void mosquitto_connect_callback_set(mosquitto mosq, on_connectDelegate);
------解决方案--------------------用Delegate.下面的例子来自msdn:
C++
typedef void (__stdcall *PFN_MYCALLBACK)();
int __stdcall MyFunction(PFN_ MYCALLBACK callback);
C#
public delegate void MyCallback();
[DllImport("MYDLL.DLL")]
public static extern void MyFunction(MyCallback callback);
msdn链接:http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx