Skip to content

Instantly share code, notes, and snippets.

@Porges
Created July 23, 2013 01:33
Show Gist options
  • Save Porges/6059207 to your computer and use it in GitHub Desktop.
Save Porges/6059207 to your computer and use it in GitHub Desktop.
Accept a handle 'inside' a .NET class...
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
struct Foo
{
private IntPtr _realFoo;
public override string ToString()
{
return "My real value is at: " + _realFoo;
}
}
class Program
{
// Declare type of callback function:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FooCallback(Foo passedObject);
// Declare native C function:
[DllImport("NativeDLL.dll")]
public static extern void InvokeCallbackWithObject(FooCallback callback);
static void Main(string[] args)
{
InvokeCallbackWithObject(MyCallback);
}
// The function receiving the object:
static void MyCallback(Foo passedObject)
{
Console.WriteLine(passedObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment