Skip to content

Instantly share code, notes, and snippets.

@radical
Last active October 5, 2021 19:30
Show Gist options
  • Save radical/4824c6a29c294766f47d01a6b8a4f7a5 to your computer and use it in GitHub Desktop.
Save radical/4824c6a29c294766f47d01a6b8a4f7a5 to your computer and use it in GitHub Desktop.
// 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