How can I import a DLL at runtime?  
Author Message
robinjam





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

I have a DLL called SampleWidget.dll, which contains a class called SampleWidget. I cannot merely import the dll at design time is because the DLL is a 'plugin' for my software. The end user will need to import more plugins as they use the software. With this in mind, How can I import the dll at runtime and create a new SampleWidget object

Thanks in advance.




.NET Development28  
 
 
Mattias Sjogren





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

Check out the Assembly.Load methods.



 
 
robinjam





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

I've loaded the DLL using an assembly object, but how do I make a new object based on the class within it

 
 
VikasGoyal





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

System.Object target = Activator.CreateInstance(className);

will create instance

className.InvokeMember("execute", System.Reflection.BindingFlags.InvokeMethod, null,target,args1);

will execute the required method.

http://DotNetWithMe.blogspot.com
vikas goyal



 
 
robinjam





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

Thanks for replying, but a moderator has moved my post from VB.NET into .NET Base class library. So therefore I guess you didn't realize I don't do C#.

When I try and convert it into VB, i get the following exception:

The given assembly name or codebase is invalid

The code:

Dim target As System.Object = Activator.CreateInstance(My.Application.Info.DirectoryPath & "\SampleWidget.dll", "SampleWidget") 'The error occurs here

target.InvokeMember("ShowWidget", System.Reflection.BindingFlags.InvokeMethod, Nothing, target, Nothing)

 

I'm guessing something in my DLL isn't set up right If so, how do I fix it



 
 
Lepaca





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

try this:

Dim a As Reflection.Assembly = Reflection.Assembly.LoadFile("FilePath")

Dim o As Object = a.CreateInstance("ClassName")

o.GetType.InvokeMember("MethodName", Reflection.BindingFlags.InvokeMethod, Nothing, o, args)


 
 
robinjam





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

Awesome! The first two parts of that code work!

However, the problems start when I try to access the ShowWidget() method.

o.GetType.InvokeMember("ShowWidget", Reflection.BindingFlags.InvokeMethod, Nothing, o, Nothing)

I get a nullreferenceexception. Any tips about how to fix it

The stack trace:

at JimmyWidgets.frmMain.frmMain_Load(Object sender, EventArgs e) in D:\Documents and Settings\Jim\My Documents\Visual Studio 2005\Projects\JimmyWidgets\JimmyWidgets\frmMain.vb:line 23
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at JimmyWidgets.frmMain.WndProc(Message& m) in D:\Documents and Settings\Jim\My Documents\Visual Studio 2005\Projects\JimmyWidgets\JimmyWidgets\frmMain.vb:line 52
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.Show()
at JimmyWidgets.frmSplash.frmSplash_Load(Object sender, EventArgs e) in D:\Documents and Settings\Jim\My Documents\Visual Studio 2005\Projects\JimmyWidgets\JimmyWidgets\frmSplash.vb:line 8
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at JimmyWidgets.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



 
 
Lepaca





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top



Awesome! The first two parts of that code work!

However, the problems start when I try to access the ShowWidget() method.

o.GetType.InvokeMember("ShowWidget", Reflection.BindingFlags.InvokeMethod, Nothing, o, Nothing)

I get a nullreferenceexception. Any tips about how to fix it



This error occurs because o is nothing! :)
In:
Dim o As Object = a.CreateInstance("ClassName")

you must specify complete ClassName.

That is, the name of class with all namespaces that contains it (MainNamespace<.SubNamespaces>.ClassName).

WARNING: in VB, you can find the main Namespace in project properties...


 
 
robinjam





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

Excellent! It works... ish.

One final question: how do I pass Me as a parameter to the method I tried putting it in the args section but it says 'cannot convert to one dimensional array of objects'.



 
 
Lepaca





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

Excellent! It works... ish.

One final question: how do I pass Me as a parameter to the method I tried putting it in the args section but it says 'cannot convert to one dimensional array of objects'.

"Me" isn't an array of objects!

Dim args() As Object = {Me}
o.GetType.InvokeMember("MethodName", Reflection.BindingFlags.InvokeMethod, Nothing, o, args)


 
 
robinjam





PostPosted: .NET Base Class Library, How can I import a DLL at runtime? Top

YESS!!! IT WORKS!!!

Thank you for your help, everyone! Without those useful tips I would have never figured it out!