C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-26-2006, 09:12 PM   #1
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,059
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.
BobS0327 is offline   Reply With Quote
Old 12-27-2006, 07:39 AM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
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
CornedBee is offline   Reply With Quote
Old 12-27-2006, 05:15 PM   #3
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,059
Quote:
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.
BobS0327 is offline   Reply With Quote
Old 12-27-2006, 06:20 PM   #4
Registered User
 
Join Date: Mar 2005
Location: Mountaintop, Pa
Posts: 1,059
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());
                }
            }
        }
BobS0327 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22