Created
November 3, 2012 10:36
-
-
Save gashtio/4006966 to your computer and use it in GitHub Desktop.
Simple C++/C# interop with a method returning a structure
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; | |
namespace Executable | |
{ | |
struct SimpleStruct | |
{ | |
public SimpleStruct(int value) | |
{ | |
Value = value; | |
} | |
public int Value; | |
} | |
class Program | |
{ | |
[DllImport("Library.dll")] | |
static extern void InstallCallback([MarshalAs(UnmanagedType.FunctionPtr)]MyCallback callback); | |
[DllImport("Library.dll")] | |
static extern void FireCallback(int x, int y); | |
delegate SimpleStruct MyCallback(int x, int y); | |
static SimpleStruct MakeResult(int x, int y) | |
{ | |
return new SimpleStruct(x + y); | |
} | |
static void Main(string[] args) | |
{ | |
InstallCallback(new MyCallback(MakeResult)); | |
Console.WriteLine("Firing callback!"); | |
FireCallback(1, 2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment