internal Delegate GetFunction(string function_name, Type signature, GetProcAddressDelegate proc_address_get_func, object proc_address_get_func_param)
 {
     lock (SyncRoot) {
         if (m_hDLL == IntPtr.Zero || function_name == null)
         {
             return(null);
         }
         IntPtr addr = IGE.Platform.Win32.API.Externals.GetProcAddress(m_hDLL, function_name);
         if (addr == IntPtr.Zero || addr == (IntPtr)1 || addr == (IntPtr)2)
         {
             if (proc_address_get_func != null)
             {
                 addr = proc_address_get_func(function_name, proc_address_get_func_param);
                 if (addr == IntPtr.Zero)
                 {
                     return(null);
                 }
             }
             else
             {
                 return(null);
             }
         }
         return(Marshal.GetDelegateForFunctionPointer(addr, signature));
     }
 }
 public static void RuntimeImport(Type type, GetProcAddressDelegate proc_address_get_func, object proc_address_get_func_param)
 {
     if (type == null)
     {
         return;
     }
     RuntimeImportInternal(null, type, proc_address_get_func, proc_address_get_func_param);
 }
        private static void RuntimeImportInternal(object instance, Type target, GetProcAddressDelegate proc_address_get_func, object proc_address_get_func_param)
        {
            FieldInfo[] fields = target.GetFields(BindingFlags.NonPublic | BindingFlags.Public | (instance == null ? BindingFlags.Static : BindingFlags.Instance));
            // GameDebugger.Log("RuntimeImport({0})", target.FullName);
            foreach (FieldInfo fi in fields)
            {
                if (!(fi.FieldType.IsSubclassOf(typeof(Delegate))))
                {
                    //GameDebugger.Log("Skipping {0} since it's type is {1}", fi.Name, fi.FieldType.FullName);
                    continue;                     // only delegates may be loaded
                }

                // get RuntimeImportAttribute attributes assigned to delegate's type
                object[] attrs = fi.FieldType.GetCustomAttributes(typeof(RuntimeImportAttribute), false);
                //GameDebugger.Log("Trying {0}", fi.Name);
                if (attrs != null)
                {
                    foreach (object attr in attrs)
                    {
                        RuntimeImportAttribute rtattr = attr as RuntimeImportAttribute;
                        string fname = (rtattr.FunctionName == null) ? fi.FieldType.Name : rtattr.FunctionName;
                        if (rtattr.LibraryName == null)
                        {
                            throw new Exception(String.Format("Null library specified for runtime loading the delegate {0}", fi.FieldType.FullName));
                        }

                        DinamicLibrary dll = null;
                        if (m_RuntimeImportedLibraries.ContainsKey(rtattr.LibraryName))
                        {
                            dll = (DinamicLibrary)m_RuntimeImportedLibraries[rtattr.LibraryName];
                        }
                        else
                        {
                            dll = new DinamicLibrary(rtattr.LibraryName);
                            m_RuntimeImportedLibraries.Add(rtattr.LibraryName, dll);
                        }

                        Delegate func = dll.Loaded ? dll.GetFunction(fname, fi.FieldType, proc_address_get_func, proc_address_get_func_param) : null;
                        if (func == null)
                        {
                            if (rtattr.Important)
                            {
                                if (dll.Loaded)
                                {
                                    throw new Exception(String.Format("Could not load at runtime an important function {0} into delegate {1} from library {2} : function does not exist", fname, fi.Name, rtattr.LibraryName));
                                }
                                else
                                {
                                    throw new Exception(String.Format("Could not load at runtime an important function {0} into delegate {1} from library {2} : library could not be loaded", fname, fi.Name, rtattr.LibraryName));
                                }
                            }
                        }
                        fi.SetValue(instance, func);
                    }
                }
            }
        }
 public static void RuntimeImport(object instance, GetProcAddressDelegate proc_address_get_func, object proc_address_get_func_param)
 {
     if (instance == null)
     {
         return;
     }
     // inline checking is done just for safety reasons ... in case other overloaded method does not get called when passing a type
     RuntimeImportInternal((instance is Type) ? null : instance, (instance is Type) ? (Type)instance : instance.GetType(), proc_address_get_func, proc_address_get_func_param);
 }
示例#5
0
 public void RuntimeImport(object instance, GetProcAddressDelegate proc_address_get_func, object proc_address_get_func_param)
 {
     throw new NotImplementedException();
 }
示例#6
0
 public void RuntimeImport(object instance, GetProcAddressDelegate proc_address_get_func, object proc_address_get_func_param)
 {
     DinamicLibrary.RuntimeImport(instance, proc_address_get_func, proc_address_get_func_param);
 }