-
Notifications
You must be signed in to change notification settings - Fork 113
/
client.cs
63 lines (53 loc) · 1.66 KB
/
client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Runtime.InteropServices;
class Awesome
{
const string libName = "awesome.so";
public struct GoSlice
{
public IntPtr data;
public long len, cap;
public GoSlice(IntPtr data, long len, long cap)
{
this.data = data;
this.len = len;
this.cap = cap;
}
}
public struct GoString
{
public string msg;
public long len;
public GoString(string msg, long len)
{
this.msg = msg;
this.len = len;
}
}
// Use DllImport to import the Awesome lib.
[DllImport(libName)]
public static extern int Add(long a, long b);
[DllImport(libName)]
public static extern double Cosine(double a);
[DllImport(libName)]
public static extern void Sort(GoSlice a);
[DllImport(libName, CharSet = CharSet.Unicode)]
public static extern void Log(GoString msg);
static void Main()
{
long add = Add(12, 99);
double cosine = Cosine(1);
long[] data = { 77, 12, 5, 99, 28, 23 };
IntPtr data_ptr = Marshal.AllocHGlobal(Buffer.ByteLength(data));
Marshal.Copy(data, 0, data_ptr, data.Length);
var nums = new GoSlice(data_ptr, data.Length, data.Length);
Sort(nums);
Marshal.Copy(nums.data, data, 0, data.Length);
string msg = "Hello from C#!";
GoString str = new GoString(msg, msg.Length);
Console.WriteLine("awesome.Add(12,99) = " + add);
Console.WriteLine("awesome.Cosine(1) = " + cosine);
Console.WriteLine("awesome.Sort(77,12,5,99,28,23): " + string.Join(", ", data));
Log(str);
}
}