![]() |
| | #1 |
| Registered User Join Date: Mar 2005 Location: Mountaintop, Pa
Posts: 1,059
| Plugin loading problem 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 | |
| | #2 |
| Cat without Hat 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 | |
| | #3 | |
| Registered User Join Date: Mar 2005 Location: Mountaintop, Pa
Posts: 1,059
| Quote:
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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |