Skip to content

Instantly share code, notes, and snippets.

@sakamoto-poteko
Created April 18, 2018 07:38
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 sakamoto-poteko/63e06a398bb79de18da368acfcf87bea to your computer and use it in GitHub Desktop.
Save sakamoto-poteko/63e06a398bb79de18da368acfcf87bea to your computer and use it in GitHub Desktop.
.Net Interop Example
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <cstdlib>
extern "C" {
struct StandardStructure {
std::int32_t a;
float b;
char *c;
};
bool ValStructure(StandardStructure s)
{
std::printf("[C] a: %d, b: %f, c: %s\n", s.a, s.b, s.c);
return true;
}
bool PtrStructure(StandardStructure *s)
{
std::printf("[C] a: %d, b: %f, c: %s\n", s->a, s->b, s->c);
return true;
}
typedef bool (*ValStructureCallback_t)(StandardStructure);
typedef bool (*PtrStructureCallback_t)(StandardStructure *);
static ValStructureCallback_t __valStructureCallback;
static PtrStructureCallback_t __ptrStructureCallback;
void SetValStrCallback(ValStructureCallback_t callback)
{
__valStructureCallback = callback;
}
void SetPtrStrCallback(PtrStructureCallback_t callback)
{
__ptrStructureCallback = callback;
}
void RunValCallback()
{
auto S = "RunValCallback";
auto s = strdup(S);
StandardStructure str;
str.a = 9999;
str.b = 2333.3333;
str.c = s;
bool ret = __valStructureCallback(str);
std::printf("[C]Result: %s", ret ? "true" : "false");
}
void RunPtrCallback()
{
auto S = "RunPtrCallback";
auto s = strdup(S);
StandardStructure str;
str.a = 8888;
str.b = 3444.4444;
str.c = s;
bool ret = __ptrStructureCallback(&str);
std::printf("[C]Result: %s", ret ? "true" : "false");
}
}
using System;
using System.Runtime.InteropServices;
namespace cs
{
[StructLayout(LayoutKind.Sequential)]
public struct Structure {
public int first;
public float second;
public String third;
}
class Program
{
[DllImport("main")]
public static extern bool ValStructure(Structure str);
[DllImport("main")]
public static extern bool PtrStructure(ref Structure str);
public delegate bool ValStructureCallback(Structure structure);
public delegate bool PtrStructureCallback(ref Structure structure);
[DllImport("main")]
public static extern bool SetValStrCallback(ValStructureCallback callback);
[DllImport("main")]
public static extern bool SetPtrStrCallback(PtrStructureCallback callback);
[DllImport("main")]
public static extern void RunValCallback();
[DllImport("main")]
public static extern void RunPtrCallback();
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var str1 = new Structure();
str1.first = 7777;
str1.second = 8888.8888f;
str1.third = "First!!!!";
SetValStrCallback(s => {
Console.WriteLine($"[#]V a: {s.first}, b: {s.second}, c: {s.third}");
return true;
});
SetPtrStrCallback((ref Structure s) => {
Console.WriteLine($"[#]P a: {s.first}, b: {s.second}, c: {s.third}");
return false;
});
RunValCallback();
RunPtrCallback();
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>
all:
g++ -shared interop.cpp -o libmain.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment