VB.NET embedded DLL in another DLL as embedded resource?
Asked Answered
G

2

4

I have seen this done in C#, such as here although, I cannot seem to figure out how to do this in VB.NET. For some background, I have created a custom ComboBox control as a .dll, and I need to implement it in another .dll(ArcMap Component).

Unfortunately, ArcMap does not allow for "third-party" DLL's to be loaded along with the component, because there is no option to reference any third-party assemblies for your add-in.

If someone could point me in the right direction, it would be more than appreciated.

Gyration answered 13/3, 2012 at 4:56 Comment(0)
P
8

We use this technique in VB.NET in Visual Studio 2008...

First, the project needs to know to include the "other" dll as an Embedded Resource. In the Solution Explorer, add the dll as a file to your project (not as a Reference). Then, open the Properties for the file and set the Build Action to "Embedded Resource." It is recommended that you create a local copy of the dll file in the structure of your project rather than linking to some other location. Once the project includes the dll file, you can then add the reference to that copy of the dll so that you can use its contents at design-time.

That ensures that the "other" dll is included in your compiled dll, but it doesn't make it automatically load when needed. That's where the following code comes in:

Public Module Core

Private _initialized As Boolean

Public Sub EnsureInitialized()
  If Not _initialized Then
    AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf AssemblyResolve
    _initialized = True
  End If
End Sub

Private Function AssemblyResolve(ByVal sender As Object, ByVal e As ResolveEventArgs) As Assembly
  Dim resourceFullName As String = String.Format("[CONTAINER ASSEMBLY].{0}.dll", e.Name.Split(","c)(0))
  Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly()
  Using resource As Stream = thisAssembly.GetManifestResourceStream(resourceFullName)
    If resource IsNot Nothing Then Return Assembly.Load(ToBytes(resource))
    Return Nothing
  End Using
End Function

Private Function ToBytes(ByVal instance As Stream) As Byte()
    Dim capacity As Integer = If(instance.CanSeek, Convert.ToInt32(instance.Length), 0)

    Using result As New MemoryStream(capacity)
        Dim readLength As Integer
        Dim buffer(4096) As Byte

        Do
            readLength = instance.Read(buffer, 0, buffer.Length)
            result.Write(buffer, 0, readLength)
        Loop While readLength > 0

        Return result.ToArray()
    End Using
End Function

End Module

Place this Module somewhere in your project and be sure to call the EnsureInitialized method to attach the AssemblyResolve handler before calling any other code in your dll.

NOTE: You'll need to replace [CONTAINER ASSEMBLY] with the name of your dll.

Also note that the above code is a stripped-down version of what we actually use because ours includes log4net logging messages at strategic places. The logging messages aren't necessary for the true functionality, so I removed them for brevity and clarity.

The major caveat to this approach is that the AssemblyResolve handler has to be attached manually. Even if you can't set things up so that EnsureInitialized is called only once during the initialization of the consuming code, you can call EnsureInitialized within any of your own modules that require the "other" dll for execution. This makes the code a little more delicate because you have to remember to make that initialization call, but it does allow you to sleep at night knowing that the dll will be available when you need it.

In my experience, some "other" dlls don't play well when they are provided as embedded resources, so you might need to play around a bit to get things working.

Final Note: I've never used ArcMap Component, so Your Mileage May Vary!

Paragraph answered 13/3, 2012 at 5:39 Comment(11)
TLS, Thank you very much. I have found some weird errors in the code you have provided. "FormatWith" is not a member of System.String, "First" is not a member of System.Array, and "ToBytes" is not a member of System.IO.Stream. Any ideas what I could replace those functions with?Gyration
Sorry about that! I had cleansed it, but didn't go far enough. There are some extension methods that we have in our code that I forgot to convert back into the standard methods. I've updated the code and added the definition of ToBytes to complete the example.Paragraph
My answer is essentially a conversion of this C# answer. I see this after reviewing that answer after having posted mine. Now we have a C# and a VB version!Paragraph
Amazing. Thank you so much. When calling this within another class, what would I use as the arguments? Sorry for the questions. Just for clarification, so I can get this right the first time.Gyration
The EnsureInitialized method has no parameters, so there's nothing to pass as arguments. You would either call Core.EnsureInitialized() or you can include the Core module in an Imports statement and just call the method. The exact contents of the Imports statement will depend on the full namespace chain in your code.Paragraph
Sorry about that, I was talking about AssemblyResolve. Is that simple AddHandler, or how is that implemented into other classes?Gyration
Ah, okay. You don't need to call AssemblyResolve directly. You would call EnsureInitialized, which adds the AssemblyResolve method as a handler for the .NET event. The true beauty of the code provided is that it's self-contained -- it encapsulates all the messy stuff and you just call the initialization method to make it work. Since we're adding it has a handler for an event, the AssemblyResolve method will be called automatically when it's needed.Paragraph
When I call 'EnsureInitialized', it does not call the subroutine of 'AssemblyResolve' I stepped through the code, it just goes right back to the form code without loading the DLL from stream. Any ideas?Gyration
@Logan - The handler method will only be invoked when the appropriate event fires. In this case, it's when the Framework is unable to find the right dll in the known places. It doesn't fire immediately when you initialize the handler.Paragraph
let us continue this discussion in chatParagraph
Nice code!!! however the .net PDF library iTextSharp.dll does not work... #21828799 :(Mathewson
A
0

I took a little bit of a different approach. I wanted something that would pretty much auto-initialize and dynamically load embedded assemblies as they were being used. I also wanted to avoid loading multiple instances of an assembly that already existed in the current AppDomain. The code below accomplishes all of those for me.

Imports System.Reflection
Imports System.Runtime.CompilerServices
''' <summary>
''' This class initializes a special AssemblyResolve handler for assemblies embedded in the current assembly's resources. <para/>
''' To auto initialize create a variable as a New EmbeddedAssemblyResolverClass in any class using an embedded assembly.
''' </summary>
Public Class EmbeddedAssemblyResolverClass
Implements IDisposable

''' <summary>
''' Initialization flag.
''' </summary>
''' <returns>[Boolean]</returns>
Public ReadOnly Property Initialized As Boolean

''' <summary>
''' Raised when successfully initialized.
''' </summary>
Public Event Initilized()

''' <summary>
''' Raised when successfully uninitialized.
''' </summary>
Public Event Uninitilized()

Sub New()
    Try
        If Not Initialized Then
            AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAppDomainAssemblies
            Initialized = True
            RaiseEvent Initilized()
        End If
    Catch ex As Exception
        'Maybe some error logging in the future.
        MsgBox(ex.Message)
    End Try
End Sub

#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls

' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
    If Not disposedValue Then
        If disposing Then
            RemoveHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAppDomainAssemblies
            _Initialized = False
            RaiseEvent Uninitilized()
        End If
    End If
    disposedValue = True
End Sub

' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
    ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
    Dispose(True)
End Sub
#End Region
End Class

Public Module EmbeddedAssemblyResolverModule

''' <summary>
''' Returns a dictionary of assemblies loaded in the current AppDomain by full name as key.
''' </summary>
''' <returns>[Dictionary(Of String, Assembly)]</returns>
Public ReadOnly Property AppDomainAssemblies As Dictionary(Of String, Assembly)
    Get
        Return AppDomain.CurrentDomain.GetAssemblies.ToDictionary(Function(a) a.FullName)
    End Get
End Property

''' <summary>
''' Method that attempts to resolve assemblies already loaded to the current AppDomain.
''' </summary>
''' <param name="sender">[Object]</param>
''' <param name="args">[ResolveEventArgs]</param>
''' <returns>[Assembly]</returns>
Public Function ResolveAppDomainAssemblies(sender As Object, args As ResolveEventArgs) As Assembly
    'Return the existing assembly if it has already been loaded into the current AppDomain.
    If AppDomainAssemblies.ContainsKey(args.Name) Then Return AppDomainAssemblies.Item(args.Name)
    'Build the potential embedded resource name.
    Dim ResourceName As String = String.Format("{0}.{1}.dll", Assembly.GetExecutingAssembly().FullName.Split(",").First, args.Name.Split(",").First)
    'Attempt to load the requested assembly from the current assembly's embedded resources.
    Return Assembly.GetExecutingAssembly.LoadEmbeddedAssembly(ResourceName)
End Function

''' <summary>
''' Loads an assembly from the current assembly's embedded resources.
''' </summary>
''' <param name="CurrentAssembly">[Assembly] Current assembly which contains the embedded assembly.</param>
''' <param name="EmbeddedAssemblyName">[String] Full name of the embedded assembly.</param>
''' <returns>[Assembly]</returns>
<Extension>
Public Function LoadEmbeddedAssembly(CurrentAssembly As Assembly, EmbeddedAssemblyName As String) As Assembly
    'Return the existing assembly if it has already been loaded into the current AppDomain.
    If AppDomainAssemblies.ContainsKey(EmbeddedAssemblyName) Then Return AppDomainAssemblies.Item(EmbeddedAssemblyName)
    'Attempt to load the requested assembly from the current assembly's embedded resources.
    Using Stream = CurrentAssembly.GetManifestResourceStream(EmbeddedAssemblyName)
        If Stream Is Nothing Then Return Nothing
        Dim RawAssembly As [Byte]() = New [Byte](Stream.Length - 1) {}
        Stream.Read(RawAssembly, 0, RawAssembly.Length)
        Return Assembly.Load(RawAssembly)
    End Using
End Function
End Module

The EmbeddedAssemblyResolverClass is used to create the actual AssemblyResolve event handler. I added a few bells and whistles by adding IDisposable support and events for Initialized and Uninitialized, but you can trim those off if not desired.

I created the rest of the code in the EmbeddedAssemblyResolverModule so that they would be global to my assembly and also because the LoadEmbeddedAssembly method is an Extension method, of which can only be created in Modules.

Now the only thing left to do is to create and instantiate the EmbeddedAssemblyResolverClass in any other class in your application that uses an assembly that is embedded into its resources.

'''' <summary>
'''' Used to auto initialize the EmbeddedAssemblyResolverClass.
'''' </summary>
Public WithEvents EAR As New EmbeddedAssemblyResolverClass

Once you call a method from an embedded resource it will first look to see if the assembly is already loaded in the current AppDomain, if it is then the assembly is returned. If the embedded assembly has not been loaded then it will be loaded dynamically from the embedded resources if it exists.

One thing that is nice about this code is that it works on assemblies that don't have an EntryPoint, like class libraries. Also I have had success in loading embedded assemblies with embedded assemblies which also used this code.

Aylesbury answered 6/11, 2016 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.