Example #1
0
 /// <summary>
 /// Link delegates fields using import declarations.
 /// </summary>
 /// <param name="type">
 /// A <see cref="System.Type"/> that specifies the type used for detecting import declarations and delegates fields.
 /// </param>
 /// <param name="sImportMap">
 /// A <see cref="T:SortedList{String, MethodInfo}"/> mapping a <see cref="MethodInfo"/> with the relative function name.
 /// </param>
 /// <param name="sDelegates">
 /// A <see cref="T:List{FieldInfo}"/> listing <see cref="FieldInfo"/> related to function delegates.
 /// </param>
 protected static void LinkOpenGLProcImports(Type type, out SortedList <string, MethodInfo> sImportMap, out List <FieldInfo> sDelegates)
 {
     LinkProcAddressImports(null, type, delegate(string libpath, string function) {
         return(GetProcAddress.GetOpenGLAddress(function));
     }, out sImportMap, out sDelegates);
 }
Example #2
0
 /// <summary>
 /// Link delegates fields using import declarations, using platform specific method for determining procedures addresses.
 /// </summary>
 /// <param name="imports">
 /// A <see cref="ImportMap"/> mapping a <see cref="MethodInfo"/> with the relative function name.
 /// </param>
 /// <param name="delegates">
 /// A <see cref="DelegateList"/> listing <see cref="FieldInfo"/> related to function delegates.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// Exception thrown if <paramref name="imports"/> or <paramref name="delegates"/> is null.
 /// </exception>
 protected static void LoadProcDelegates(ImportMap imports, DelegateList delegates)
 {
     LoadProcDelegates(String.Empty, imports, delegates, delegate(string libpath, string function) {
         return(GetProcAddress.GetOpenGLAddress(function));
     });
 }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="imports"></param>
        /// <param name="delegateField"></param>
        /// <returns></returns>
        private static Delegate GetDelegate(ImportMap imports, FieldInfo delegateField)
        {
            if (imports == null)
            {
                throw new ArgumentNullException("imports");
            }

            Delegate importDelegate;

            Attribute[] aliasOfAttributes = Attribute.GetCustomAttributes(delegateField, typeof(AliasOfAttribute));
            string      importName        = delegateField.Name.Substring(1);
            IntPtr      importAddress     = IntPtr.Zero;

            #region Get Function Pointer

            if (aliasOfAttributes.Length > 0)
            {
                for (int i = 0; i < aliasOfAttributes.Length; i++)
                {
                    if ((importAddress = GetProcAddress.GetOpenGLAddress(((AliasOfAttribute)aliasOfAttributes[i]).SymbolName)) != IntPtr.Zero)
                    {
                        break;
                    }
                }
            }
            else
            {
                importAddress = GetProcAddress.GetOpenGLAddress(importName);
            }

            #endregion

            // Is function implemented?
            if (importAddress == IntPtr.Zero)
            {
                return(null);
            }

            // Try to load external symbol
            if ((importDelegate = Marshal.GetDelegateForFunctionPointer(importAddress, delegateField.FieldType)) == null)
            {
                MethodInfo importMethod = null;

                if (aliasOfAttributes.Length > 0)
                {
                    for (int i = 0; i < aliasOfAttributes.Length; i++)
                    {
                        if ((importAddress = GetProcAddress.GetOpenGLAddress(((AliasOfAttribute)aliasOfAttributes[i]).SymbolName)) != IntPtr.Zero)
                        {
                            break;
                        }

                        if (imports.TryGetValue(((AliasOfAttribute)aliasOfAttributes[i]).SymbolName, out importMethod))
                        {
                            break;
                        }
                    }
                }
                else
                {
                    imports.TryGetValue(importName, out importMethod);
                }

                return(importMethod != null ? Delegate.CreateDelegate(delegateField.FieldType, importMethod) : null);
            }
            else
            {
                return(importDelegate);
            }
        }