コード例 #1
0
 static IntPtr dlsym(IntPtr handle, string symbol)
 {
     if (Platform.RunningOS == OS.Linux)
     {
         return(LinuxInterop.dlsym(handle, symbol));
     }
     else
     {
         return(MacInterop.dlsym(handle, symbol));
     }
 }
コード例 #2
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);
            }
        }