Thread: Plugin loading problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058

    Plugin loading problem

    I keep getting this error on the following code when I attempt to load my plugins from the Plugins folder. If I move my plugins out of the Plugin folder and into the Application.StartupPath, the problem disappears. In other words, if I try to load the plugins from c:\utils\C#\WinZm\bin\Debug\Plugins, the app generates the error. But if I load them from c:\utils\C#\WinZm\Bin\Debug (the ApplicationStartupPath) the plugins load without any problem. Any suggestions for a resolution?

    Attachment 7096

    Code:
    private void Form1_Load(object sender, System.EventArgs e)
            {
                string path = Application.StartupPath + @"\Plugins";
                //string path = "C:\\utils\\C#\\WinZM\\bin\\Debug";
                MessageBox.Show(path);
                string[] pluginFiles = Directory.GetFiles(path, "*.dll");
                ipi = new IPlugin[pluginFiles.Length];
                for (int i = 0; i < pluginFiles.Length; i++)
                {
                    string args = pluginFiles[i].Substring(
                        pluginFiles[i].LastIndexOf("\\") + 1,
                        pluginFiles[i].IndexOf(".dll") -
                        pluginFiles[i].LastIndexOf("\\") - 1);
                    Type ObjType = null;
                    try
                    {
                        // load it
                        Assembly ass = null;
                        ass = Assembly.Load(args);
                        if (ass != null)
                        {
                            ObjType = ass.GetType(args + ".PlugIn");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    try
                    {
                        // Create the object as we have the Report Type
                        if (ObjType != null)
                        {
                            ipi[i] = (IPlugin)Activator.CreateInstance(ObjType);
                            ipi[i].Host = this;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
    Last edited by BobS0327; 12-10-2007 at 07:50 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why don't you keep the full path of the DLL? Without it, it just follows DLL lookup rules, and your plugin folder apparently isn't in the search path.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Why don't you keep the full path of the DLL? Without it, it just follows DLL lookup rules, and your plugin folder apparently isn't in the search path.
    I tried a full path for the dll and it generates the following error:
    Attachment 7101

    I remember coming across a site that explained a quirk with the Assembly Load method a while back when I was Googling for plugin info. I didn't pay much attention to the site because at the time I didn't have any plugin code. Now I just can't find that site.
    Last edited by BobS0327; 12-10-2007 at 07:50 PM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Ahh, found it!!! The problem is that the Load method uses the Framework discovery rules. I.e., the Global Assembly Cache (GAC), the path in the app's manifest, the directory where your app resides etc. If the plugin is outside of the discovery rules, as in my case where it was located below the app's directory, it will not load. The solution is to use the LoadFile method which will always load the path requested.

    Code:
    private void Form1_Load(object sender, System.EventArgs e)
            {
                string args1;
                string path = Application.StartupPath + @"\Plugins\";
                string[] pluginFiles = Directory.GetFiles(path, "*.dll");
                ipi = new IPlugin[pluginFiles.Length];
             
                for (int i = 0; i < pluginFiles.Length; i++)
                {
                        string args = pluginFiles[i].Substring(
                        pluginFiles[i].LastIndexOf("\\") + 1,
                        pluginFiles[i].IndexOf(".dll") -
                        pluginFiles[i].LastIndexOf("\\") - 1);
                        Type ObjType = null;
                    try
                    {
                        Assembly asm;
                        args1 = path + args + ".dll";
                        asm = Assembly.LoadFile(args1);
                        Type[] types = asm.GetTypes();
    
                        if (asm != null)
                        {
                            ObjType = asm.GetType(args + ".PlugIn");
                                ipi[i] = (IPlugin)Activator.CreateInstance(ObjType);
                                ipi[i].Host = this;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Loading Bitmaps in Allegro
    By LiNeAr in forum Game Programming
    Replies: 1
    Last Post: 08-15-2005, 04:12 PM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. Loading chars from files problem
    By Korhedron in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2002, 03:00 PM
  5. problem printing out structure after loading
    By pari in forum C Programming
    Replies: 17
    Last Post: 11-23-2002, 09:12 PM