Question

I have a native api function which looks like:

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
);

I have translated it to C# as:

[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);

The callback function looks like:

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

I am guessing the C# version will look something like:

public delegate void WLAN_NOTIFICATION_CALLBACK(
    IntPtr pWlanNotificationData, IntPtr pContext)

Essentially I have two questions:

Is a delegate the correct object to use for a native method that expects a function pointer?

And if so, will this automatically call the delegate in c# when the notification is received?

Was it helpful?

Solution

Yes, you should use a delegate, and yes, it will work fine.

However, you must make sure that the GC does not collect your delegate instance. (typically by putting it in a field)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top