//From Managed.Windows.Forms/XplatUI static bool IsRunningOnMac() { IntPtr buf = IntPtr.Zero; try { buf = Marshal.AllocHGlobal(8192); // This is a hacktastic way of getting sysname from uname () if (NativeUnixMehods.uname(buf) == 0) { string os = Marshal.PtrToStringAnsi(buf); if (os == "Darwin") { return(true); } } } catch { } finally { if (buf != IntPtr.Zero) { Marshal.FreeHGlobal(buf); } } return(false); }
public static void GetLibraryMethod <T>(SupportedPlatform platform, IntPtr handle, string name, out T t) { IntPtr result; switch (platform) { case SupportedPlatform.Windows: result = NativeWindowsMethods.GetProcAddress(handle, name); if (result == IntPtr.Zero) { throw new EntryPointNotFoundException(name, GetLastWindowsError()); } break; case SupportedPlatform.Linux: case SupportedPlatform.MacOSX: result = NativeUnixMehods.dlsym(handle, name); if (result == IntPtr.Zero) { throw new EntryPointNotFoundException(name, GetLastErrorUnix()); } break; default: throw new NotSupportedException(); } t = (T)(object)Marshal.GetDelegateForFunctionPointer(result, typeof(T)); }
private static bool LoadDynamicLibrary(SupportedPlatform platform, string name, out IntPtr handle, out string location) { switch (platform) { case SupportedPlatform.Windows: handle = NativeWindowsMethods.LoadLibrary(name); if (handle == IntPtr.Zero) { goto default; } location = GetLocationWindows(handle) ?? name; return(true); case SupportedPlatform.Linux: case SupportedPlatform.MacOSX: handle = NativeUnixMehods.dlopen(name, 2 /* RTLD_NOW */); if (handle == IntPtr.Zero) { goto default; } location = GetLocationUnix(handle) ?? name; return(true); default: handle = IntPtr.Zero; location = null; return(false); } }
public static void UnloadDynamicLibrary(SupportedPlatform platform, IntPtr handle) { if (platform != SupportedPlatform.Android) { throw new NotSupportedException(); } if (NativeUnixMehods.dlclose(handle) != 0) { throw GetLastError() ?? new InvalidOperationException(); } }
private static string GetLocationUnix(IntPtr handle) { IntPtr symbol = NativeUnixMehods.dlsym(handle, "ts3client_freeMemory"); NativeUnixMehods.Dl_info dl_info = new NativeUnixMehods.Dl_info(); if (symbol == IntPtr.Zero || NativeUnixMehods.dladdr(symbol, ref dl_info) == 0) { return(null); } return(dl_info.dli_fname); }
public static void GetLibraryMethod <T>(SupportedPlatform platform, IntPtr handle, string name, out T t) { if (platform != SupportedPlatform.Android) { throw new NotSupportedException(); } IntPtr result = NativeUnixMehods.dlsym(handle, name); if (result == IntPtr.Zero) { throw new EntryPointNotFoundException(name, GetLastError()); } t = (T)(object)Marshal.GetDelegateForFunctionPointer(result, typeof(T)); }
public static void LoadDynamicLibrary(SupportedPlatform platform, string[] possibleNames, out IntPtr handle, out string location) { if (platform != SupportedPlatform.Android) { throw new NotSupportedException(); } foreach (string possibleName in possibleNames) { handle = NativeUnixMehods.dlopen(possibleName, 2 /* RTLD_NOW */); if (handle != IntPtr.Zero) { location = possibleName; return; } } string message = string.Join(", ", possibleNames); throw new DllNotFoundException(message, GetLastError()); }
public static void UnloadDynamicLibrary(SupportedPlatform platform, IntPtr handle) { switch (platform) { case SupportedPlatform.Windows: if (NativeWindowsMethods.FreeLibrary(handle) == false) { throw GetLastWindowsError(); } break; case SupportedPlatform.Linux: case SupportedPlatform.MacOSX: if (NativeUnixMehods.dlclose(handle) != 0) { throw GetLastErrorUnix() ?? new InvalidOperationException(); } break; default: throw new NotSupportedException(); } }
private static Exception GetLastError() { IntPtr pointer = NativeUnixMehods.dlerror(); return(pointer != IntPtr.Zero ? new InvalidOperationException(Marshal.PtrToStringAnsi(pointer)) : null); }
private static Exception GetLastErrorUnix() { string message = NativeUnixMehods.dlerror(); return(message != null ? new InvalidOperationException(message) : null); }