You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This sample illustrates the use of NativeLibrary APIs to implement library name mappings similar to the [Mono](https://www.mono-project.com/)[Dllmap](http://www.mono-project.com/docs/advanced/pinvoke/dllmap/) feature.
4
+
5
+
## NativeLibrary APIs
6
+
7
+
.Net Core 3.0 provides a rich set of APIs to manage native libraries:
8
+
9
+
-[NativeLibrary APIs](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.nativelibrary?view=netcore-3.0): Perform operations on native libraries (such as `Load()`, `Free()`, get the address of an exported symbol, etc.) in a platform-independent way from managed code.
10
+
-[DllImport Resolver callback](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.nativelibrary.setdllimportresolver?view=netcore-3.0): Gets a callback for first-chance native library resolution using custom logic.
11
+
-[Native Library Resolve event](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext.resolvingunmanageddll?view=netcore-3.0): Get an event for last-chance native library resolution using custom logic.
12
+
13
+
## Library Mapping
14
+
15
+
These APIs can be used to implement custom native library resolution logic, including DllMap, as illustrated in this example. The sample demonstrates:
16
+
17
+
- An [app](Demo.cs) that pInvokes a method in `OldLib`, but runs in an environment where only [`NewLib`](NewLib.cpp) is available.
18
+
- The [XML file](Demo.xml) that maps the library name from `OldLib` to `NewLib`.
19
+
- The [Map](Map.cs) implementation, which parses the above mapping and uses `NativeLibrary` APIs to load the correct library.
20
+
21
+
## Build and Run
22
+
23
+
1. Install .NET Core 3.0 Preview 3 or newer.
24
+
25
+
2. Use the .NET Core SDK to build the project via `dotnet build`.
26
+
27
+
3. Build the native component `NewLib.cpp` as a dynamic library, using the platform's native toolset.
28
+
29
+
Place the generated native library (`NewLib.dll` / `libNewLib.so` / `libNewLib.dylib`) in the `dotnet build` output directory.
0 commit comments