Click here to Skip to main content
15,749,090 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hey all,

Seems there is a lot of info on this topic but none that actually solve my problem. I am writing a plugin based program with external dlls. My program loads the dlls dynamically (loading from a specific directory) and I do not know what they are prior to compiling.

I need to be able to call a function from that dll. I can specify the name of the function for each plugin (aka, GetInfo or whatever) but I am not sure how to create an instance of the function in the plugin.

Here is what I have so far.

VB
For Each dll In dlls
           Try
               extAssembly = System.Reflection.Assembly.LoadFrom(dll.FullName)
               extForms(index) = extAssembly.CreateInstance("myPlugin.frmMain", True)

           Catch ex As Exception
           End Try
       Next


And then later I can do a simple...

VB
extForms(index).Showdialog()


But how do I create an object that I can link to a specific function either in my form or a separate class dynamically?

Thanks! :)
Posted

OK, I've done this in different variants. Please see my sketch of design here:

Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].

Correct way of recognition of plug-ins is by some plug-in interface. Doing it by name is absolutely unsupportable.

—SA
 
Share this answer
 
Comments
Espen Harlinn 1-Jun-11 18:34pm    
Good point, my 5
Sergey Alexandrovich Kryukov 1-Jun-11 18:42pm    
Thank you, Espen.
--SA
Ok, I was able to find and call a function. Question. Is there a way to directly invoke my function "Test" instead of For Each'ing through the entire dll?

VB
Dim assem As System.Reflection.Assembly =System.Reflection.Assembly.LoadFrom("myplugin.dll")
Dim ty As Type = assem.GetType("myPlugin.frmMain")
Dim class1 As Object = Activator.CreateInstance(ty)
Dim strMsg As String
Dim myMember() As System.Reflection.MethodInfo = class1.GetType.GetMethods()
For Each member As System.Reflection.MethodInfo In myMember
     If member.Name = "Test" Then
          strMsg = member.Invoke(Nothing, Nothing, Nothing, Nothing, Nothing)
          Exit For
     End If
Next
 
Share this answer
 
Comments
Dave Kreskowiak 1-Jun-11 16:45pm    
Yeah, instead of calling GetMethods, use GetMethod("name") instead.
Sergey Alexandrovich Kryukov 1-Jun-11 17:14pm    
No! Don't use GetMethod by name, never! The supportable way is through interface (known by both host and plug-in).

See my solution.
--SA
Dave Kreskowiak 1-Jun-11 18:07pm    
I know an Interface would be prefferable. This is just the simple replacement for his code instead of going back and recoding everything to support an interface.
Sergey Alexandrovich Kryukov 1-Jun-11 20:18pm    
Sure, I understand that. I just don't want to mislead OP.
--SA
JBenhart 1-Jun-11 17:42pm    
Hey SA, is there a reason to not use Dave's method? It does work. I looked over your solutions and unfortunately, once again, they are over my head. I don't understand interfaces and assemblies, etc. If I create a plugin architecture that has a basic form to load and a few methods to return data, can you tell me the drawbacks?

My plugin is a simple form. It creates an assembly of the form and then does a showdialog() on run. I just need a title, description, and bitmap from the dll.

Lastly, if you could elaborate with specific code examples, that may help. :)

Thanks to both of you! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900