tags:

views:

1244

answers:

5

Hi All,

I have to make a graphical user interface application using the language of my choice. The application will run on Windows XP. It will be some sort of a complex windows form application. I think and as per most suggestions, C# will be the best to use. The tree structure on the left of the GUI will populate after reading from a configuration file which will be a binary file . (but initially I can work with a simple ASCII file to test my code.). The application will accept some inputs from the user through this GUI and will write the back to the same config file and will reflect the changes in the tree structure or the labels or any other pertaining field on the form.

There will be 3 tabs and 3 corresponding config files for each of the tabs. I need some help designing the application for now. I am planning to make a host application (main application) and use the 3 tab controls as plugins. Is this workable ? If so can you please guide me on this. I mean how do I make 3 plugins in C# and how do I write the interfaces so that the main application knows which plugin to load and when to load it ? Will there be a separate “Plugin” folder under my project folder ? I hope you got my point though this is too little of an information for you to begin with.

Also there are some .cpp files already existing in the project. These files along with some .h files contain some important definitions and constants in them. These need to be integrated with my C# application. I have no clue how to do that but I am sure that it is possible by compiling the .cpp code in a .dll and then exposing the compiled .dll to my C# application. Please let me know if you need some more information for the top level design.

Thanks, Viren

+8  A: 

One of the easiest plugin concepts I have ever used was certainly the Managed Extensibility Framework which will be part of .NET 4 (afaik). Unfortunately it is not yet finished and only a preview is available which may differ from the final version. That being said, we used MEF Preview 3 for a uni project and it worked without problems and it certainly made the whole plugin stuff a lot easier.

Joey
Definitely use MEF it's awesome.
dr. evil
+1  A: 

Take a look to Castle.

jmservera
thanks..castle seems fantastic..i was not aware of it..
VP
A: 

.NET Framework use COM model in its guts. See http://blog.caljacobson.com/2007/07/26/creating-a-plug-in-framework-in-c-resources/ for a list of plugin example using this techique.

J-16 SDiZ
+3  A: 

Look at the System.Addin namespace : http://msdn.microsoft.com/en-us/library/system.addin.aspx

Otherwise you can do everything yourself. Before this namespace was available, I used a common interface "IPlugin" that every plugin/addin needed to use. I then had a loader which inspected all the *.dll in a folder then used reflection to check for the interface. I could then create instances of classes which implemented my plugin/addin interface

The cpp files will probably need converting to c#, or you could possibly create a dll to reference.

eschneider
+6  A: 

To implement a plugin interface manually, you will need a method something like this. I've left some TODOs in, where you would want to enhance the error handling and/or make the implementation a little more case specific.

    public List<T> LoadPlugin<T>(string directory)
    {
        Type interfaceType = typeof(T);
        List<T> implementations = new List<T>();

        //TODO: perform checks to ensure type is valid

        foreach (var file in System.IO.Directory.GetFiles(directory))
        {
            //TODO: add proper file handling here and limit files to check
            //try/catch added in place of ensure files are not .dll
            try
            {
                foreach (var type in System.Reflection.Assembly.LoadFile(file).GetTypes())
                {
                    if (interfaceType.IsAssignableFrom(type) && interfaceType != type)
                    { 
                        //found class that implements interface
                        //TODO: perform additional checks to ensure any
                        //requirements not specified in interface
                        //ex: ensure type is a class, check for default constructor, etc
                        T instance = (T)Activator.CreateInstance(type);
                        implementations.Add(instance);
                    }
                }
            }
            catch { }
        }

        return implementations;
    }

Example to call:

List<IPlugin> plugins = LoadPlugin<IPlugin>(path);

As for the c++ part of your question. There are few different ways you could approach this, though the correct choice depends on your specific situation. You can make a clr compliant .dll in c++, which your c# project could reference and call like any other .dll it references. Additionally, you could use P/Invoke to call into a native .dll.

Timothy Carter
thanks a lot.this really helped..
VP