Using an async .NET method as a Win32 API callback

Viewed 110

I am using Win32 API inside a .NET Core application. I call Win32 API function with a callback parameter:

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate void CALLBACK(StructType data);


[DllImport("somewindows.dll", SetLastError = false, ExactSpelling = true)]
public static extern HRESULT RegisterWin32Callback(CALLBACK CallbackAsync);


private async void CallbackAsync(StructType data)
{
    await MyFuncAsync();
}

RegisterWin32Callback(CallbackAsync);

I need to call async .NET methods from a callback, so I am trying to make a callback to be async too.

The parameter in the Win32 API callback is defined as:

IN StructType data

However, as soon as in, out and ref are not supported in .NET async methods, I removed "in" form .NET declaration. My understanding that IN does not play any role in terms of marshaling.

Are there any potential issues with such an approach?

0 Answers
Related