Created
July 23, 2013 01:33
-
-
Save Porges/6059207 to your computer and use it in GitHub Desktop.
Accept a handle 'inside' a .NET class...
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
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