-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTest_NativeArray.cs
More file actions
61 lines (48 loc) · 1.95 KB
/
Test_NativeArray.cs
File metadata and controls
61 lines (48 loc) · 1.95 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
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Linq;
using System.Runtime.InteropServices;
using GCore.NativeInterop;
namespace LibNamespace
{
public static class Test_NativeArray
{
#region Test_NativeArray_StructFixed_CS
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Args
{
public fixed int Arr[8];
public int Multiplier;
}
[UnmanagedCallersOnly]
public static unsafe int Test_NativeArray_StructFixed(Args args)
{
int ret = 0;
for(int i = 0; i < 8; i++)
{
ret += args.Arr[i] * args.Multiplier;
}
return ret;
}
#endregion // ----------------------------------------------------------------------------------------------------
#region Test_NativeArray_ArgumentFixed_CS
[UnmanagedCallersOnly]
public static int Test_NativeArray_ArgumentFixed(IntPtr arrPtr, int multiplier)
{
// ArrPointer as argument does not work here!
ArrPointer8<int> arr = new ArrPointer8<int>(arrPtr);
return arr.Sum(el => el * multiplier); ;
}
#endregion // ----------------------------------------------------------------------------------------------------
#region Test_NativeArray_ArgumentFixed_FunctionPointer_CS
public delegate int FunctionPointerCallbackDelegate(ArrPointer8<int> arr, int multiplier);
public static FunctionPointerCallbackDelegate FunctionPointerCallbackDelegateInstance =
new FunctionPointerCallbackDelegate(Callback);
public static int Callback(ArrPointer8<int> arr, int multiplier) => arr.Sum(el => el * multiplier);
[UnmanagedCallersOnly]
public static IntPtr Test_NativeArray_ArgumentFixed_FunctionPointer()
{
return Marshal.GetFunctionPointerForDelegate(FunctionPointerCallbackDelegateInstance);
}
#endregion
}
}