Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call a DLL dynamically

Status
Not open for further replies.

clegg

Programmer
Jan 25, 2001
98
GB
I've got an app written in VB.NET that creates a menu structure, with each menu item being a module in our application. We are in the process of converting the old VB6 stuff to .NET so there are a mixture of things going off!

Basically, if it's a VB6.0 app I can do this:

Dim a As Object
a = CreateObject(ProgramToCall)
a.StartAtTheEntryPoint

How do I do a similar thing for programs written in .NET?

I can set a reference to all the DLL's that have been written but I'm using a single event handler for the menu click event and would like to call the program on the fly based on properties i've saved in the Tag (e.g. ProgramToCall).

TIA
Clegg
 
You can still use CreateObject in the same way.

Better yet, try the VB6 upgrade in .Net and this should answer most of your questions.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Hi Ron

I think you've misunderstood what I want to do as you cannot use CreateObject to instantiate .NET assemblies.

I've found a solution that works however:

Dim a As String = My.Settings.AssemblyPath & "TestClass.dll"
Dim b As String = "TestClass.ExternalProg"

Dim MyAssembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFrom(a)

Dim TypeToLoad As Type = MyAssembly.GetType(b)

Dim GenericInstance As Object
GenericInstance = Activator.CreateInstance(TypeToLoad)

' There has to be a method called 'ShowForm' in all objects
' In this instance it's being passed a reference to the
' current form as it's an MDI app and the new object needs
' to add itself as an MDI child

GenericInstance.ShowForm(Me)

In my production model, a & b will be passed as variables rather then hard coded as in this example but it show how it would work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top