languages | products | page_type | name | urlFragment | description | |||
---|---|---|---|---|---|---|---|---|
|
|
sample |
DllMap Demo |
dllmap-demo |
A sample that illustrates the use of NativeLibrary APIs to implement library name mappings |
This sample illustrates the use of NativeLibrary APIs to implement library name mappings similar to the Mono Dllmap feature.
.NET Core 3.1 provides a rich set of APIs to manage native libraries:
- NativeLibrary APIs: 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. - DllImport Resolver callback: Gets a callback for first-chance native library resolution using custom logic.
- Native Library Resolve event: Get an event for last-chance native library resolution using custom logic.
These APIs can be used to implement custom native library resolution logic, including DllMap, as illustrated in this example. The sample demonstrates:
- An app that pInvokes a method in
OldLib
, but runs in an environment where onlyNewLib
is available. - The XML file that maps the library name from
OldLib
toNewLib
. - The Map implementation, which parses the above mapping and uses
NativeLibrary
APIs to load the correct library.
-
Install .NET Core 3.1 or newer.
-
Use the .NET Core SDK to build the project via
dotnet build
. -
Build the native component
NewLib.cpp
as a dynamic library, using the platform's native toolset.Place the generated native library (
NewLib.dll
/libNewLib.so
/libNewLib.dylib
) in thedotnet build
output directory. -
Run the app with
dotnet run