static IntPtr dlsym(IntPtr handle, string symbol)
 {
     if (Platform.RunningOS == OS.Linux)
     {
         return(LinuxInterop.dlsym(handle, symbol));
     }
     else
     {
         return(MacInterop.dlsym(handle, symbol));
     }
 }
 static void dlclose(IntPtr handle)
 {
     if (Platform.RunningOS == OS.Linux)
     {
         LinuxInterop.dlclose(handle);
     }
     else
     {
         MacInterop.dlclose(handle);
     }
 }
 static IntPtr dlopen(string filename, int flags)
 {
     if (Platform.RunningOS == OS.Linux)
     {
         return(LinuxInterop.dlopen(filename, flags));
     }
     else
     {
         return(MacInterop.dlopen(filename, flags));
     }
 }
 static IntPtr dlerror()
 {
     if (Platform.RunningOS == OS.Linux)
     {
         return(LinuxInterop.dlerror());
     }
     else
     {
         return(MacInterop.dlerror());
     }
 }
Пример #5
0
        public static IntPtr LoadLibrary(string libName, string libraryPath)
        {
            var libHandler     = IntPtr.Zero;
            var libNameWithExt = string.Empty;

            if (string.IsNullOrEmpty(libName) || string.IsNullOrEmpty(libraryPath))
            {
                return(libHandler);
            }

            SetLibraryDirectory(libraryPath);

            var libraryFiles = Directory.GetFiles(libraryPath);

            foreach (var libraryFile in libraryFiles)
            {
                if (libraryFile.EndsWith(".meta"))
                {
                    continue;
                }

                var fileName = Path.GetFileName(libraryFile);

                if (fileName.StartsWith(libName + ".") && (string.IsNullOrEmpty(libNameWithExt) || fileName.Any(char.IsDigit)))
                {
                    libNameWithExt = fileName;
                }
            }

            switch (UMPSettings.RuntimePlatform)
            {
            case UMPSettings.Platforms.Win:
                libHandler = WindowsInterop.LoadLibrary(Path.Combine(libraryPath, libNameWithExt));
                break;

            case UMPSettings.Platforms.Mac:
                libHandler = MacInterop.dlopen(Path.Combine(libraryPath, libNameWithExt), LIN_RTLD_NOW);
                break;

            case UMPSettings.Platforms.Linux:
                libHandler = LinuxInterop.dlopen(Path.Combine(libraryPath, libNameWithExt), LIN_RTLD_NOW);
                break;
            }

            if (libHandler == IntPtr.Zero)
            {
                throw new Exception(string.Format("[LoadLibrary] Can't load '{0}' library", libName));
            }

            return(libHandler);
        }
Пример #6
0
        public static T GetLibraryDelegate <T>(IntPtr handler)
        {
            string functionName      = null;
            var    procAddress       = IntPtr.Zero;
            var    supportedPlatform = UMPSettings.RuntimePlatform;

            try
            {
                var attrs = typeof(T).GetCustomAttributes(typeof(NativeFunctionAttribute), false);
                if (attrs.Length == 0)
                {
                    throw new Exception("[GetLibraryDelegate] Could not find the native attribute type.");
                }

                var attr = (NativeFunctionAttribute)attrs[0];
                functionName = attr.FunctionName;
                if (_interopDelegates.ContainsKey(functionName))
                {
                    return((T)Convert.ChangeType(_interopDelegates[attr.FunctionName], typeof(T), null));
                }

                if (supportedPlatform == UMPSettings.Platforms.Win)
                {
                    procAddress = WindowsInterop.GetProcAddress(handler, attr.FunctionName);
                }
                if (supportedPlatform == UMPSettings.Platforms.Mac)
                {
                    procAddress = MacInterop.dlsym(handler, attr.FunctionName);
                }
                if (supportedPlatform == UMPSettings.Platforms.Linux)
                {
                    procAddress = LinuxInterop.dlsym(handler, attr.FunctionName);
                }

                if (procAddress == IntPtr.Zero)
                {
                    throw new Exception(string.Format("[GetLibraryDelegate] Can't get process address from '{0}'", handler));
                }

                var delegateForFunctionPointer = Marshal.GetDelegateForFunctionPointer(procAddress, typeof(T));
                _interopDelegates[attr.FunctionName] = delegateForFunctionPointer;
                return((T)Convert.ChangeType(delegateForFunctionPointer, typeof(T), null));
            }
            catch (Exception e)
            {
                throw new MissingMethodException(string.Format("[GetLibraryDelegate] The address of the function '{0}' does not exist in '{1}' library.", functionName, handler), e);
            }
        }
Пример #7
0
        public static bool FreeLibrary(IntPtr handler)
        {
            switch (UMPSettings.RuntimePlatform)
            {
            case UMPSettings.Platforms.Win:
                return(WindowsInterop.FreeLibrary(handler));

            case UMPSettings.Platforms.Mac:
                return(MacInterop.dlclose(handler) == 0);

            case UMPSettings.Platforms.Linux:
                return(LinuxInterop.dlclose(handler) == 0);
            }

            return(false);
        }