I am creating a program that uses a very simple plugin system. This is the code I use to download possible plugins:
public interface IPlugin { string Name { get; } string Description { get; } bool Execute(System.Windows.Forms.IWin32Window parent); } private void loadPlugins() { int idx = 0; string[] pluginFolders = getPluginFolders(); Array.ForEach(pluginFolders, folder => { string[] pluginFiles = getPluginFiles(folder); Array.ForEach(pluginFiles, file => { try { System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(file); Array.ForEach(assembly.GetTypes(), type => { if(type.GetInterface("PluginExecutor.IPlugin") != null) { IPlugin plugin = assembly.CreateInstance(type.ToString()) as IPlugin; if(plugin != null) lista.Add(new PluginItem(plugin.Name, plugin.Description, file, plugin)); } }); } catch(Exception) { } }); }); }
When the user selects a specific plugin from the list, I run the plugin's Execute method. So far, so good! As you can see, the plugins are loaded from the folder, and inside the folder there are several dlls that are necessary, but the plugin. My problem is that I canβt make the plugin βseeβ the DLL, it just looks for the application launch launcher folder, but not the folder where the plugin was loaded.
I tried several methods: 1. Change the current directory to the plugins folder. 2. Using the interoperational call of SetDllDirectory 3. Adding an entry to the registry to indicate the folder in which I want to look at it (see the code below)
None of these methods work. What am I missing? When I load the dll plugin dynamically, it does not seem to obey any of the above methods. What else can I try?
Regards, MartinH.
//HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths Microsoft.Win32.RegistryKey appPaths = Microsoft.Win32.Registry.LocalMachine.CreateSubKey( string.Format( @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\{0}", System.IO.Path.GetFileName(Application.ExecutablePath)), Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree); appPaths.SetValue(string.Empty, Application.ExecutablePath); object path = appPaths.GetValue("Path"); if(path == null) appPaths.SetValue("Path", System.IO.Path.GetDirectoryName(pluginItem.FileName)); else { string strPath = string.Format("{0};{1}", path, System.IO.Path.GetDirectoryName(pluginItem.FileName)); appPaths.SetValue("Path", strPath); } appPaths.Flush();
source share