I'm working on porting a .Net Core 3.1 application from windows to Linux. I have a ourdevice.dll on windows, and the equivalent ourdevice.so built for Linux. This dll is a native dll that we are using on windows with a pinvoke wrapper.
We load the functions from the native dll using DllImports from kernel32.dll
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary", SetLastError = true)]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
We create delegates for each of the funtions to import:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate short AdcOpen([MarshalAs(UnmanagedType.LPStr)] string adcName, [MarshalAs(UnmanagedType.LPStr)] string protocol, [MarshalAs(UnmanagedType.LPStr)] string port, ref short handle, byte performSwReset);
private AdcOpen adcOpen;
Then we map all the native functions something like this:
IntPtr pAddressOfFunction;
pDll = LoadLibrary(LibraryName);
// check if library is loaded
if (pDll != IntPtr.Zero)
{
// get proc address of the native functions
pAddressOfFunction = GetProcAddress(pDll, "AdcOpen");
if (pAddressOfFunction != IntPtr.Zero)
adcOpen = (AdcOpen)Marshal.GetDelegateForFunctionPointer(pAddressOfFunction, typeof(AdcOpen));
else
message += "Function AdcOpen not found\n";
//snip loading all the exported functions
}
In the Linux application, obviously, we can't use kernel32.dll. We are writing C# in .Net core on Linux, and we have the native module ourdevice.so. How can I import the functions from the native Linux module into our C# wrapper? Is it even possible?