Friday, April 18, 2014

Dynamically load DLL

  Dim myDllLoaded As DLLWrapper
   Dim oDll As DLLWrapper = New DLLWrapper(sTypeName, sDLLName, sDLLFilePath)

     If myDllLoaded IsNot Nothing Then
                                            ' Activate method in the DLL for processing email
                                            If Not myDllLoaded.ActivatedOnce Then
                                                If Not myDllLoaded.ActivateDLLType() Then
                                                    Throw New Exception(String.Format("Not able to load DLL {0}", oEmailType.TypeName))
                                                End If
                                            End If
                                            If myDllLoaded.ActivatedOnce Then
                                                Dim aryEmail(0) As WenOutlookEmail
                                                aryEmail(0) = myEmail
                                                ' Process each email
                                                If myDllLoaded.ProcessTasks(aryEmail) Then
                                                    ' Delete email after it has been processed successfull

The class below called DLLWrapper

Imports System.Reflection
Imports System.IO
Imports System.Xml
''' <summary>
''' Wrapper to get all necessary information to load DLL and activate method of DLL.
''' </summary>
Public Class DLLWrapper

#Region "Constants"
    Private Const METHOD_NAME As String = "MyMethod_in_DLL"
#End Region

#Region "Attributes"
    Private m_bActivatedOnce As Boolean
    Private m_iTryToActivateCount As Integer
    Private m_oAssembly As Assembly
    Private m_sTypeName As String
    Private m_sDLLName As String
    Private m_sDLLFilePath As String
    Private m_oType As Object
    Private m_oMethodInfo As MethodInfo
    Private m_oDLL As Object
#End Region

#Region "Properties"
    Public ReadOnly Property ActivatedOnce() As Boolean
        Get
            Return m_bActivatedOnce
        End Get
    End Property
#End Region

#Region "Public methods"

    Public Sub New(ByVal sTypeName As String, ByVal sDLLName As String, ByVal sDllFilePath As String)
        Dim sClassAndMethodName As String = String.Empty
        Dim sFullName As String
        Try
            m_bActivatedOnce = False
            m_iTryToActivateCount = 0
            m_sTypeName = String.Empty
            m_sDLLName = String.Empty
            m_sDLLFilePath = String.Empty
            Dim oAssemblyName As AssemblyName = New AssemblyName(sDLLName)
            oAssemblyName.CodeBase = sDllFilePath
            m_oAssembly = Assembly.Load(oAssemblyName)
            sFullName = sTypeName
            m_oType = m_oAssembly.GetType(sFullName)
            m_oMethodInfo = m_oType.GetMethod(METHOD_NAME, New Type() {GetType(myDLLObject)})
        Catch ex As Exception
        End Try

    End Sub

    ''' <summary>
    ''' Invoke method in DLL to process.
    ''' </summary>
    ''' <param name="aryEmail">The array of email object.</param>
    ''' <returns></returns>
    Public Function ProcessTasks(ByVal aryObj() As myDLLObject) As Boolean // all DLL has the same method name ProcessTasks
        Dim bRc As Boolean = False
        Try
            If m_oMethodInfo.Invoke(m_oDLL, aryObj) Then // calls MyMethod_in_DLL in each DLL(dynamiclly loaded) and pass aryObj
                bRc = True
            End If
        Catch ex As Exception
        End Try
        Return bRc
    End Function

    ''' <summary>
    ''' Activates(creates) an instance of the DLL
    ''' </summary>
    ''' <returns></returns>
    Public Function ActivateDLLType() As Boolean
        Dim bRC As Boolean = False
        Try
            If Not m_bActivatedOnce Then
                m_oDLL = Activator.CreateInstance(m_oType, xxxxx) //xxxxx is the parm for new() method, it could be a logger object
                If m_oDLL IsNot Nothing Then
                    m_bActivatedOnce = True
                    bRC = True
                End If
            End If
        Catch ex As Exception
        End Try
        Return bRC
    End Function
#End Region

End Class

No comments:

Post a Comment