ColinChang.InteropNet
1.0.1
dotnet add package ColinChang.InteropNet --version 1.0.1
NuGet\Install-Package ColinChang.InteropNet -Version 1.0.1
<PackageReference Include="ColinChang.InteropNet" Version="1.0.1" />
paket add ColinChang.InteropNet --version 1.0.1
#r "nuget: ColinChang.InteropNet, 1.0.1"
// 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
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- System.Reflection.Emit (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
update the name of libdl.so to libdl.so.2