Skip to content

Instantly share code, notes, and snippets.

@gashtio
Created November 3, 2012 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gashtio/4006966 to your computer and use it in GitHub Desktop.
Save gashtio/4006966 to your computer and use it in GitHub Desktop.
Simple C++/C# interop with a method returning a structure
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