Add DLL at runtime.


T

Tomas Andersson

I have a problem.

I'm making an app in VB9 VS 2008.
The app is going to be a form MDI and a menu.

What I need to do is search app.path/Plugin or something like that and find
..DLLs.
My DLLs will have a couple of functions.

First I need the Main form to add a menuitem in the menu.
This menu item should then declare an instance of a form in the DLL as an
MDIchild and open the form.

I have looked high and low on the internet and MSDN to find what I need
without success.

As I'm writing both the main program and the DLLs that make up the add-in
functionality I'm thinking I don't need to make an interface between the
main app and the DLL.
But when I try referencing the DLL directly which works and then remove the
DLL I get a file missing error.

Does anyone have some insights?
Code examples would be appreciated. Or where I can find literature regarding
my problem.
 
Ad

Advertisements

G

Guramrit Singh

Hi,
This is how you can do this,
You need the following architecture:
1. a interface (say IBaseForm) declared in base class library (say
Project1).
2. The plugin form which should implement IBaseForm in your Plugin Project
(say Plugin1), same in other plugins, reference to Project1 should be added.
3. Main Application Project (say WindowsFormApplication1) with following
code in MenuItemClick function (reference to Project1 should be added):
Dim ofd As New OpenFileDialog()
If ofd.ShowDialog() = DialogResult.OK Then
Dim formToShow As Form = Nothing
Dim assembly As Assembly = Assembly.LoadFile(ofd.FileName)
For Each tmpType As Type In assembly.GetTypes()
For Each iface As Type In tmpType.GetInterfaces()
If iface = GetType(IBaseForm) Then
formToShow = DirectCast(Activator.CreateInstance(tmpType), Form)
Exit For
End If
Next
Next
If formToShow <> Nothing Then
formToShow.MdiParent = Me
formToShow.Show()
Else
MessageBox.Show("Invalid Plugin")
End If
End If

You can reduce above code using LINQ. Hope this can help, (Please don't
forget to rate this answer).
 
T

Tomas Andersson

OK.
I created a new Windosws forms project
Made the form IsMdiContainer = True
and added a button

added this code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call openplugin()
End Sub

Private Sub openplugin()
Dim ofd As New OpenFileDialog()
If ofd.ShowDialog() = DialogResult.OK Then
Dim formToShow As Form = Nothing
Dim assembly As Reflection.Assembly =
Reflection.Assembly.LoadFile(ofd.FileName)
For Each tmpType As Type In assembly.GetTypes()
For Each iface As Type In tmpType.GetInterfaces()
If iface = GetType(IBaseForm) Then
formToShow =
DirectCast(Activator.CreateInstance(tmpType), Form)
Exit For
End If
Next
Next

If formToShow <> Nothing Then
formToShow.MdiParent = Me
formToShow.Show()
Else
MessageBox.Show("Invalid Plugin")
End If
End If
End Sub
End Class

added two new projects
Project1 class project with IBaseForm.vb

Namespace IBaseForm

End Namespace

And plugin1

Public Class plugin1

End Class

added reference to project1 in windows form project and plugin1

and I get an Type 'IBaseForm' is not defined error
and Operator '<>' is not defined for types 'System.Windows.forms.form' and
'system.windows.forms.form'

Where did I go wrong???
 
O

Onur Güzel

OK.
I created a new Windosws forms project
Made the form IsMdiContainer = True
and added a button

added this code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call openplugin()
End Sub

Private Sub openplugin()
Dim ofd As New OpenFileDialog()
If ofd.ShowDialog() = DialogResult.OK Then
Dim formToShow As Form = Nothing
Dim assembly As Reflection.Assembly =
Reflection.Assembly.LoadFile(ofd.FileName)
For Each tmpType As Type In assembly.GetTypes()
For Each iface As Type In tmpType.GetInterfaces()
If iface = GetType(IBaseForm) Then
formToShow =
DirectCast(Activator.CreateInstance(tmpType), Form)
Exit For
End If
Next
Next

If formToShow <> Nothing Then
formToShow.MdiParent = Me
formToShow.Show()
Else
MessageBox.Show("Invalid Plugin")
End If
End If
End Sub
End Class

added two new projects
Project1 class project with IBaseForm.vb

Namespace IBaseForm

End Namespace

And plugin1

Public Class plugin1

End Class

added reference to project1 in windows form project and plugin1

and I get an Type 'IBaseForm' is not defined error
and Operator '<>' is not defined for types 'System.Windows.forms.form' and
'system.windows.forms.form'

Where did I go wrong???

Try "IsNot'' instead of inequality operator that is ''<>''.

HTH,

Onur Guzel
 
Ad

Advertisements

O

Onur Güzel

OK.
I created a new Windosws forms project
Made the form IsMdiContainer = True
and added a button

added this code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call openplugin()
End Sub

Private Sub openplugin()
Dim ofd As New OpenFileDialog()
If ofd.ShowDialog() = DialogResult.OK Then
Dim formToShow As Form = Nothing
Dim assembly As Reflection.Assembly =
Reflection.Assembly.LoadFile(ofd.FileName)
For Each tmpType As Type In assembly.GetTypes()
For Each iface As Type In tmpType.GetInterfaces()
If iface = GetType(IBaseForm) Then
formToShow =
DirectCast(Activator.CreateInstance(tmpType), Form)
Exit For
End If
Next
Next

If formToShow <> Nothing Then
formToShow.MdiParent = Me
formToShow.Show()
Else
MessageBox.Show("Invalid Plugin")
End If
End If
End Sub
End Class

added two new projects
Project1 class project with IBaseForm.vb

Namespace IBaseForm

End Namespace

And plugin1

Public Class plugin1

End Class

added reference to project1 in windows form project and plugin1

and I get an Type 'IBaseForm' is not defined error
and Operator '<>' is not defined for types 'System.Windows.forms.form' and
'system.windows.forms.form'

Where did I go wrong???

Try ''IsNot'' instead of inequality operator that is ''<>''.

HTH,

Onur Guzel
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

MDI Child from DLL 2
Debugging main app with compiled DLL 2
Class library( dll) 7
DLL compatibility issue 1
Splash Screen in DLL 3
DLL 1
Deploying with Custom References 5
"Optional" reference? 2

Top