Wednesday, December 12, 2012

How could you dynamically load your own DLL

In Visual Basic Express 2010, Visual Studio 2010 or any Visual Basic Edition, you could dynamically load any compatible DLL file. The sample presented is separated into several parts:


  1. The main program
  2. The DLL number 1
  3. The DLL number 2



You may have to change the path to get your DLL. I intentionally left the big string in the code so you could easily change it before using it.

This is the Layout sample:

  •   Form1
  •   MenuStrip1
  •   Multiples Menu Items (Fichier, Options, HQ, ….)


Sample Assembly Form Layout




Sample of form1.vb



Imports System.Reflection 'you need this line to use Assembly and Activator
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        load_dll()

    End Sub


    Private Sub load_dll()
        Dim oType As System.Type
        Dim oAssembly As System.Reflection.Assembly
        Dim oObject As System.Object
        Dim ms As System.Windows.Forms.ToolStripMenuItem

        'We load the assembly called HelloWorld.dll located in C:\
        oAssembly = Assembly.LoadFrom("C:\Users\checkkay\Documents\Visual Studio 2010\Projects\SampleAssembly\ClassLibrary1\bin\Debug\ClassLibrary1.dll")
        'We get the type of the application (Class_Root_Namespace.Name_Class)
        oType = oAssembly.GetType("ClassLibrary1.SayHello")
        'We create an type instance
        oObject = Activator.CreateInstance(oType)
        'We execute the methode Hello
        MsgBox(oObject.Hello()) 'call a command from the class1


        tsHQ1 = New System.Windows.Forms.ToolStripMenuItem("fonction 1")



       ms = Me.MenuStrip1.Items(0) 'Selecting the first menu Item (File or Fichier)
        ms.DropDownItems.Add(tsHQ1) 'Adding a MenuItem Under File or Fichier



        oAssembly = Assembly.LoadFrom("C:\Users\checkkay\Documents\Visual Studio 2010\Projects\SampleAssembly\ClassLibrary2\bin\Debug\ClassLibrary2.dll")
        'We get the type of the application (Class_Root_Namespace.Name_Class)
        oType = oAssembly.GetType("ClassLibrary2.SayHello")
        'WE create an type instance
        oObject = Activator.CreateInstance(oType)
        tsHQ2 = New System.Windows.Forms.ToolStripMenuItem("fonction 2")

        ms = Me.MenuStrip1.Items(0) 'Selecting the first menu Item (File or Fichier)
        ms.DropDownItems.Add(tsHQ2) 'Adding a MenuItem Under File or Fichier


    End Sub

    Private Sub FermerToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles FermerToolStripMenuItem.Click
        Me.Close()
        Me.Dispose()

    End Sub

End Class




Sample of place where you could declare your ToolStripMenuItem with events:

  • tsHQ1 as System.Windows.Forms.ToolStripMenuItem
  • tsHQ2 as System.Windows.Forms.ToolStripMenuItem




Imports System.Reflection
Imports System

Module Module1
    Public WithEvents tsHQ1 As System.Windows.Forms.ToolStripMenuItem
    Public WithEvents tsHQ2 As System.Windows.Forms.ToolStripMenuItem
    Public Sub launchetshq1() Handles tsHQ1.MouseDown
        MsgBox("allo 1")
    End Sub

    Public Sub launchetshq2() Handles tsHQ2.MouseDown
        MsgBox("allo 2")
    End Sub
End Module






Finally a sample of a home made DLL. ClassLibrary1 and ClassLibrary2 are virtually identical.


Public Class SayHello
    Private mName As String

    Function Hello()
        Return "Hello World"
    End Function

    Public Function Bonjour()
        Return "Bonjour le monde"
    End Function



    Public Sub New()
        Me.mName = "HQtool1"
    End Sub
End Class






What the code does is relatively simple.


First, the form will try to lead a first DLL and will put it in oAssembly. oAssembly will be our DLL inside the main program.

Then, you need to access to your stuff inside your DLL (Assembly). Here, you don’t have any choice, you need to know the Class Name, the Assembly Name; if required the Name Space and the root Name Space.

Root Namespace


Class Name



      oType = oAssembly.GetType("ClassLibrary1.SayHello")



Download the sample project : SampleAssembly.zip


Other posts: 
Add Control in your form dynamically


References and External Links:

Visual Studio 2012 from Microsoft Visual Studio site



Cheapest way to get Visual Studio any editions from Amazon:



           


1 comment:

  1. No se puede cargar el archivo o ensamblado 'file:///Ruta.dll' ni una de sus dependencias. Se esperaba que el módulo tuviera un manifiesto de ensamblado.

    ReplyDelete