我使用的是第三方C库,它提供了方法和一些事件处理程序。我想订阅一个或多个事件,但我不知道如何结合在C#中导入C库来实现这一点。
方法不是问题。例如,我使用以下代码
[C++]
API ResultType DoSometing(DeviceHandle devHndl);
[C#]
[DllImport("Some.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern ErrorType DoSomething(IntPtr devHndl);对于这些事件,我有以下定义:
[C++]
typedef ResultType (CALLING_CONVENTION *FPtr_GetStatus)(DeviceHandle);
typedef void (CALLING_CONVENTION *FPtr_DeviceEventHandler)(DeviceHandle devHndl, DeviceEvent event, void *context);
API FPtr_DeviceEventHandler RegisterSomeEventHandler(FPtr_DeviceEventHandler handler, void *context);你知道如何在C#代码中处理这个问题吗?
发布于 2014-02-21 23:59:59
这些事件处理程序实际上是回调函数。因此,您必须在C#端使用委托。
沿着这些思路的东西..
delegate ResultType GetStatusDelegate(IntPtr deviceHandle);
delegate void DeviceEventDelegate(IntPtr deviceHandle, DeviceEvent e, IntPtr context);
[DllImport("Some.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void RegisterSomeEventHandler(DeviceEventDelegate handler, IntPtr context);上面的例子遗漏了ResultType和DeviceEvent的声明,这在问题中是不可用的。这些也必须声明。
发布于 2014-02-22 00:02:24
https://stackoverflow.com/questions/21939092
复制相似问题