-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReturnFunctionPointer.cs
More file actions
50 lines (38 loc) · 1.56 KB
/
Copy pathReturnFunctionPointer.cs
File metadata and controls
50 lines (38 loc) · 1.56 KB
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
using System;
using System.Text;
using JSIL.Runtime;
using System.Runtime.InteropServices;
public static unsafe class Program {
[StructLayout(LayoutKind.Sequential)]
public struct TestStruct {
public int I;
public float F;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate TestStruct TReturnStructArgument (TestStruct arg);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int TWriteStringIntoBuffer (byte* dest, int capacity);
[DllImport("common.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern IntPtr ReturnWriteStringIntoBuffer ();
[DllImport("common.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr ReturnReturnStructArgument ();
public static void Main () {
using (var na = new NativePackedArray<byte>(512)) {
var pFunction = ReturnWriteStringIntoBuffer();
var d = Marshal.GetDelegateForFunctionPointer<TWriteStringIntoBuffer>(pFunction);
int numBytes;
fixed (byte* pBuffer = na.Array) {
numBytes = d(pBuffer, na.Length);
}
var s = Encoding.ASCII.GetString(na, 0, numBytes);
Console.WriteLine("'{0}'", s);
}
{
var pFunction = ReturnReturnStructArgument();
var d = Marshal.GetDelegateForFunctionPointer<TReturnStructArgument>(pFunction);
var a = new TestStruct { I = 3, F = 5.5f };
var b = d(a);
Console.WriteLine("i={0} f={1:F4}", b.I, b.F);
}
}
}