Last active
October 5, 2021 19:30
-
-
Save radical/4824c6a29c294766f47d01a6b8a4f7a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Licensed to the .NET Foundation under one or more agreements. | |
// The .NET Foundation licenses this file to you under the MIT license. | |
using System; | |
using System.Threading.Tasks; | |
using System.Runtime.InteropServices; | |
using System.Runtime.CompilerServices; | |
public class Test | |
{ | |
public unsafe static int Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
delegate* unmanaged[Cdecl]<int, int> unmanagedPtr = &Callback; | |
NativeFunctionWithCallback((IntPtr)unmanagedPtr); | |
return args.Length; | |
} | |
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })] | |
public unsafe static int Callback(int i) | |
{ | |
Console.WriteLine ($"-- managed callback called with i: {i}"); | |
return i * i; | |
} | |
[DllImport("NativeLib")] | |
private unsafe static extern void NativeFunctionWithCallback(IntPtr callback); | |
//private unsafe static extern void NativeFunctionWithCallbackUsingFunctionPointers(delegate* unmanaged[Cdecl]<int, int> callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment