-
Notifications
You must be signed in to change notification settings - Fork 4
/
Test_DllImport.cs
33 lines (27 loc) · 1.09 KB
/
Test_DllImport.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
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace LibNamespace
{
public static class Test_DllImport
{
// begin-snippet: Test_DllImport_CS
private static IntPtr s_moduleHandle = IntPtr.Zero;
private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
IntPtr ret = libraryName == "Test_DllImport_LibName" ? s_moduleHandle : NativeLibrary.Load(libraryName, assembly, searchPath);
//Console.WriteLine($"ImportResolver s_moduleHandle={s_moduleHandle} ret={ret}");
return ret;
}
[DllImport("Test_DllImport_LibName")]
public static extern int Test_DllImport_ExternC(int number);
[UnmanagedCallersOnly]
public static int Test_DllImport_Call(IntPtr moduleHandle, int number)
{
s_moduleHandle = moduleHandle;
NativeLibrary.SetDllImportResolver(typeof(Test_DllImport).Assembly, ImportResolver);
return Test_DllImport_ExternC(number);
}
// end-snippet
}
}