As a user, I would like to have a NativeLibraryLoader for Linux/MacOS #35

Open
opened 2026-02-06 12:36:09 +01:00 by frederik.engelhardt · 1 comment
No description provided.
Author
Owner

Example Code:

using System;
using System.Runtime.InteropServices;

public static class LinuxNative
{
    private const string LibDl = "libdl.so.2";

    [DllImport(LibDl)]
    public static extern IntPtr dlopen(string fileName, int flags);

    [DllImport(LibDl)]
    public static extern IntPtr dlsym(IntPtr handle, string symbol);

    [DllImport(LibDl)]
    public static extern int dlclose(IntPtr handle);

    [DllImport(LibDl)]
    public static extern IntPtr dlerror();

    public const int RTLD_LAZY = 0x0001;
    public const int RTLD_NOW = 0x0002;
}

Usage:

IntPtr handle = LinuxNative.dlopen("libm.so.6", LinuxNative.RTLD_NOW);

if (handle == IntPtr.Zero)
{
    IntPtr errPtr = LinuxNative.dlerror();
    string error = Marshal.PtrToStringAnsi(errPtr);
    Console.WriteLine(error);
}

Alternative for .net 5+

System.Runtime.InteropServices.NativeLibrary

Example Code: ``` using System; using System.Runtime.InteropServices; public static class LinuxNative { private const string LibDl = "libdl.so.2"; [DllImport(LibDl)] public static extern IntPtr dlopen(string fileName, int flags); [DllImport(LibDl)] public static extern IntPtr dlsym(IntPtr handle, string symbol); [DllImport(LibDl)] public static extern int dlclose(IntPtr handle); [DllImport(LibDl)] public static extern IntPtr dlerror(); public const int RTLD_LAZY = 0x0001; public const int RTLD_NOW = 0x0002; } ``` Usage: ``` IntPtr handle = LinuxNative.dlopen("libm.so.6", LinuxNative.RTLD_NOW); if (handle == IntPtr.Zero) { IntPtr errPtr = LinuxNative.dlerror(); string error = Marshal.PtrToStringAnsi(errPtr); Console.WriteLine(error); } ``` Alternative for .net 5+ `System.Runtime.InteropServices.NativeLibrary`
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
equadrat-oss/Framework.Core#35
No description provided.