Exemple #1
0
        internal T GetInteropDelegate <T>()
        {
            string vlcFunctionName = null;

            try
            {
                var attrs = typeof(T).GetCustomAttributes(typeof(LibVlcFunctionAttribute), false);
                if (attrs.Length == 0)
                {
                    throw new Exception("Could not find the LibVlcFunctionAttribute.");
                }
                var attr = (LibVlcFunctionAttribute)attrs[0];
                vlcFunctionName = attr.FunctionName;
                if (myInteropDelegates.ContainsKey(vlcFunctionName))
                {
                    return((T)Convert.ChangeType(myInteropDelegates[attr.FunctionName], typeof(T), null));
                }
                var procAddress = Win32Interops.GetProcAddress(myLibVlcDllHandle, attr.FunctionName);
                if (procAddress == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                var delegateForFunctionPointer = Marshal.GetDelegateForFunctionPointer(procAddress, typeof(T));
                myInteropDelegates[attr.FunctionName] = delegateForFunctionPointer;
                return((T)Convert.ChangeType(delegateForFunctionPointer, typeof(T), null));
            }
            catch (Win32Exception e)
            {
                throw new MissingMethodException(string.Format("The address of the function '{0}' does not exist in libvlc library.", vlcFunctionName), e);
            }
        }
Exemple #2
0
        internal T GetInteropDelegate <T>()
        {
            string vlcFunctionName = null;

            try
            {
#if NETSTANDARD1_3
                var attrs = typeof(T).GetTypeInfo().GetCustomAttributes(typeof(LibVlcFunctionAttribute), false).ToArray();
#else
                var attrs = typeof(T).GetCustomAttributes(typeof(LibVlcFunctionAttribute), false);
#endif
                if (attrs.Length == 0)
                {
                    throw new Exception("Could not find the LibVlcFunctionAttribute.");
                }
                var attr = (LibVlcFunctionAttribute)attrs[0];
                vlcFunctionName = attr.FunctionName;

                lock (this._myInteropDelegatesLockObject)
                {
                    if (myInteropDelegates.ContainsKey(vlcFunctionName))
                    {
                        return((T)myInteropDelegates[attr.FunctionName]);
                    }

                    var procAddress = Win32Interops.GetProcAddress(myLibVlcDllHandle, attr.FunctionName);
                    if (procAddress == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }

                    var delegateForFunctionPointer = MarshalHelper.GetDelegateForFunctionPointer <T>(procAddress);

                    myInteropDelegates[attr.FunctionName] = delegateForFunctionPointer;

                    return(delegateForFunctionPointer);
                }
            }
            catch (Win32Exception e)
            {
                throw new MissingMethodException(string.Format("The address of the function '{0}' does not exist in libvlc library.", vlcFunctionName), e);
            }
        }