Any method marked with UnmanagedCallersOnlyAttribute can be directly called from native code. The function token can be loaded to a local variable using the address-of operator in C# and passed as a callback to a native method.
public ref class UnmanagedCallersOnlyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)]
public sealed class UnmanagedCallersOnlyAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, Inherited=false)>]
type UnmanagedCallersOnlyAttribute = class
inherit Attribute
Public NotInheritable Class UnmanagedCallersOnlyAttribute
Inherits Attribute
The following example demonstrates passing a callback marked UnmanagedCallersOnlyAttribute to a native function.
[DllImport("NativeLibrary")]
internal static extern unsafe void NativeFunctionWithCallback(delegate* unmanaged[Cdecl]<int, int> callback);
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static int DoubleInt(int i) => i * 2;
public static unsafe void PassCallbackToNativeFunction()
{
NativeFunctionWithCallback(&DoubleInt);
}
Methods marked with this attribute have the following restrictions:
static
.Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. In this articleWas this page helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4