Skip to content

Instantly share code, notes, and snippets.

@cashlalala
Forked from andrewloable/Program.cs
Created March 17, 2021 01:19
Show Gist options
  • Save cashlalala/eb96d3f30e81c7a11b9d9c6c55d4a087 to your computer and use it in GitHub Desktop.
Save cashlalala/eb96d3f30e81c7a11b9d9c6c55d4a087 to your computer and use it in GitHub Desktop.

Revisions

  1. @andrewloable andrewloable created this gist Jun 23, 2019.
    22 changes: 22 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    using System;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace GoSharedDLL
    {
    class Program
    {
    [DllImport("shared.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
    private static extern IntPtr ReturnReversedString(byte[] input);

    static void Main(string[] args)
    {
    var input = Encoding.UTF8.GetBytes("Hello World!");
    var resultbytes = ReturnReversedString(input);
    var output = Marshal.PtrToStringAnsi(resultbytes);
    Console.WriteLine(output);
    Console.WriteLine("Press enter to continue...");
    Console.ReadLine();
    }
    }
    }
    18 changes: 18 additions & 0 deletions shared.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    package main

    import "C"

    //export ReturnReversedString
    func ReturnReversedString(input *C.char) *C.char {
    str := C.GoString(input)
    runes := []rune(str)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
    runes[i], runes[j] = runes[j], runes[i]
    }
    retval := string(runes)
    return C.CString(retval)
    }

    func main() {}

    // compile using go build -o shared.dll -buildmode=c-shared