Skip to content

Instantly share code, notes, and snippets.

@sakamoto-poteko
Created April 18, 2018 07:38
Show Gist options
  • Select an option

  • Save sakamoto-poteko/63e06a398bb79de18da368acfcf87bea to your computer and use it in GitHub Desktop.

Select an option

Save sakamoto-poteko/63e06a398bb79de18da368acfcf87bea to your computer and use it in GitHub Desktop.

Revisions

  1. sakamoto-poteko created this gist Apr 18, 2018.
    2 changes: 2 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    all:
    g++ -shared interop.cpp -o libmain.so
    68 changes: 68 additions & 0 deletions interop.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #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");
    }

    }
    72 changes: 72 additions & 0 deletions interop.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    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();
    }
    }
    }

    16 changes: 16 additions & 0 deletions interop.csproj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    <Project Sdk="Microsoft.NET.Sdk">



    <PropertyGroup>

    <OutputType>Exe</OutputType>

    <TargetFramework>netcoreapp2.0</TargetFramework>

    </PropertyGroup>



    </Project>