ColinChang.InteropNet 1.0.1

.NET Standard 2.1
dotnet add package ColinChang.InteropNet --version 1.0.1
NuGet\Install-Package ColinChang.InteropNet -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ColinChang.InteropNet" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ColinChang.InteropNet --version 1.0.1
#r "nuget: ColinChang.InteropNet, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ColinChang.InteropNet as a Cake Addin
#addin nuget:?package=ColinChang.InteropNet&version=1.0.1

// Install ColinChang.InteropNet as a Cake Tool
#tool nuget:?package=ColinChang.InteropNet&version=1.0.1

InteropNet

The library allows you to work with native libraries. Standard approach with the DllImport attribute may be inconvenient if you want to build AnyCPU assembly with MS.NET/Mono support. The Interoptor class can generate implementation of interface with target signatures of native methods.

For example, let's create a native library (NativeLib) with the function int sum(int a, int b) { return a + b; } and build it in four configuration (Windows/Unix, x86/x64):

x86/NativeLib.dll
x86/libNativeLib.so
x64/NativeLib.dll
x64/libNativeLib.so

Now we can write the following code:

public interface INative
{
    [RuntimeDllImport("NativeLib", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sum")]
    int Sum(int a, int b);
}

class Program
{
    static void Main()
    {
        var native = Interoptor.Create<INative>();
        Console.WriteLine("2 + 3 = " + native.Sum(2, 3));
    }
}

In the program, we declared interface INative with signatures of the target native methods. Each signature should be marked with the RuntimeDllImport attribute with name of a native library and other options. The instance of Interoptor helped us to create instance of the INative interface on the fly. The implementation of the sum method call corresponding native method from library that correspond to current architecture and OS. The Interoptor generates the following code:

namespace Interoptor.NativeInstance
{
  [UnmanagedFunctionPointer(CallingConvention.Cdecl, BestFitMapping = false, CharSet = (CharSet) 0, SetLastError = false, ThrowOnUnmappableChar = false)]
  [StructLayout(LayoutKind.Auto, CharSet = CharSet.Auto)]
  public delegate int SumDelegate(int a, int b);

  public class NativeImplementation : INative
  {
    private SumDelegate SumField;

    public NativeImplementation(LibraryLoader loader)
    {
      IntPtr dllHandle = loader.LoadLibrary("NativeLib", (string) null);
      this.SumField = (SumDelegate) Marshal.GetDelegateForFunctionPointer(loader.GetProcAddress(dllHandle, "sum"), typeof (SumDelegate));
    }

    public int Sum(int a, int b)
    {
      return this.SumField(a, b);
    }
  }
} 

As a result, we received a single .NET cross-platform AnyCPU-program with calls of native methods because of the LibraryLoader class loaded handles for specific user environment.

Platform

Only Windows and Linux(CentOS 8,Debian 9,Ubuntu 20.04 are tested) are supported.

We use libdl.so.2 to load library(*.so), it means some Linux release version like alpine doesn't support it.

the libdl.so.2 file is a library in most of Linux. Generally,it's in a system path configured in system environment variables.

Thanks

https://github.com/AndreyAkinshin/InteropDotNet

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 520 12/1/2020
1.0.0 360 12/1/2020

update the name of libdl.so to libdl.so.2