2

我有一个本机 api 函数,如下所示:

DWORD WINAPI WlanRegisterNotification(
  __in        HANDLE hClientHandle,
  __in        DWORD dwNotifSource,
  __in        BOOL bIgnoreDuplicate,
  __in_opt    WLAN_NOTIFICATION_CALLBACK  funcCallback,
  __in_opt    PVOID pCallbackContext,
  __reserved  PVOID pReserved,
  __out_opt   PDWORD pdwPrevNotifSource
);

我已将其翻译为 C#:

[DllImport("Wlanapi.dll", EntryPoint = "WlanRegisterNotification")]
public static extern uint WlanRegisterNotification(
     IntPtr hClientHandle, WLAN_NOTIFICATION_SOURCE dwNotifSource,
     bool bIgnoreDuplicate, WLAN_NOTIFICATION_CALLBACK funcCallback,
     IntPtr pCallbackContext, IntPtr pReserved,
     [Out] out WLAN_NOTIFICATION_SOURCE pdwPrevNotifSource);

回调函数如下所示:

typedef VOID ( WINAPI *WLAN_NOTIFICATION_CALLBACK)(
  PWLAN_NOTIFICATION_DATA data,
  PVOID context
);

我猜 C# 版本看起来像:

public delegate void WLAN_NOTIFICATION_CALLBACK(
    IntPtr pWlanNotificationData, IntPtr pContext)

基本上我有两个问题:

委托是用于需要函数指针的本机方法的正确对象吗?

如果是这样,这会在收到通知时自动调用 c# 中的委托吗?

4

1 回答 1

3

是的,您应该使用委托,是的,它可以正常工作。

但是,您必须确保 GC 不会收集您的委托实例。(通常是把它放在一个字段中)

于 2011-06-17T14:19:30.633 回答