Skip to content

Latest commit

 

History

History

DllMapDemo

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
languages products page_type name urlFragment description
csharp
cpp
dotnet
sample
DllMap Demo
dllmap-demo
A sample that illustrates the use of NativeLibrary APIs to implement library name mappings

DllMap Demo

This sample illustrates the use of NativeLibrary APIs to implement library name mappings similar to the Mono Dllmap feature.

NativeLibrary APIs

.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.

Library Mapping

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 only NewLib is available.
  • The XML file that maps the library name from OldLib to NewLib.
  • The Map implementation, which parses the above mapping and uses NativeLibrary APIs to load the correct library.

Build and Run

  1. Install .NET Core 3.1 or newer.

  2. Use the .NET Core SDK to build the project via dotnet build.

  3. 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 the dotnet build output directory.

  4. Run the app with dotnet run