I'm working on making a set of native libraries for various platforms (OS X, Windows, iOS, and Android). On OS X, I have a bundle. For Windows, I opened the native dll with the following code:
internal static void LoadNativeDll(string FileName)
{
if (lib != IntPtr.Zero)
{
return;
}
lib = LoadLibrary(FileName);
if (lib == IntPtr.Zero)
{
Debug.LogError("Failed to load native library! ");
}
}
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr LoadLibrary(
string lpFileName
);
I am trying the following for OS X:
public static IntPtr LoadLibrary(string fileName) {
return dlopen(fileName, RTLD_NOW);
}
public static void FreeLibrary(IntPtr handle) {
dlclose(handle);
}
public IntPtr GetProcAddress(IntPtr dllHandle, string name) {
// clear previous errors if any
dlerror();
var res = dlsym(dllHandle, name);
var errPtr = dlerror();
if (errPtr != IntPtr.Zero) {
throw new Exception("dlsym: " + Marshal.PtrToStringAnsi(errPtr));
}
return res;
}
const int RTLD_NOW = 2;
[DllImport("libdl.so")]
private static extern IntPtr dlopen(String fileName, int flags);
[DllImport("libdl.so")]
private static extern IntPtr dlsym(IntPtr handle, String symbol);
[DllImport("libdl.so")]
private static extern int dlclose(IntPtr handle);
[DllImport("libdl.so")]
private static extern IntPtr dlerror();
This is based on a [blog I found][1]. OS X is UNIX based, but there does not appear to be a version of libdl.so compiled for OS X and thus get the error:
DllNotFoundException: libdl.so
Even if one did exist, would it need to be packaged within the asset folder? MacMono seems to be an option that [used to exist][2]. Help, how do people load libraries in OS X?
[1]: http://dimitry-i.blogspot.com/2013/01/mononet-how-to-dynamically-load-native.html?showComment=1450473004077
[2]: http://lipsky.me/blog/2012/7/21/calling-a-dynamic-library-from-monomac-part-1
↧