VB.NET embedded DLL in another DLL as an embedded resource?

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

Unfortunately, ArcMap doesn’t allow you to load third-party DLL files with the component, because there is no way to reference any third-party assemblies for your add-in.

If someone can point me in the right direction, this will be more than appreciated.

+4
source share
2 answers

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

First, the project must know in order to include the "other" dll as an embedded resource. In Solution Explorer, add the dll to the file in your project (and not as a link). Then open the “Properties for the file” and set the “Build actions” to “Embedded resource”. It is recommended that you create a local copy of the dll file in the structure of your project, and not contact any other location. After the project includes the DLL file, you can add a link to this copy of the dll so that you can use its contents during development.

This ensures that the "other" DLL is included in your compiled dll, but it does not load automatically. This is 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 

Put this module somewhere in your project, and be sure to call the EnsureInitialized method to bind the AssemblyResolve handler before calling any other code in your DLL.

NOTE. You 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 our system includes log4net logging of messages in strategic places. Log messages are not needed for true functionality, so I deleted them for brevity and clarity.

The main caveat to this approach is that the AssemblyResolve handler must be connected manually. Even if you cannot configure everything so that EnsureInitialized is called only once during the initialization of the consumption code, you can call EnsureInitialized in any of your own modules, which require a different dll to execute. This makes the code a bit more subtle because you have to remember to make this initialization call, but it allows you to sleep at night knowing that the DLL will be available when you need it.

In my experience, some "other" DLLs do not play very well when they are provided as embedded resources, so you may need to play a little to get it to work.

Final note: I have never used an ArcMap component, so your mileage may vary!

+9
source

I took a slightly different approach. I wanted something that pretty much automatically initialized and dynamically loaded inline assemblies when they were used. I also wanted to avoid loading multiple instances of the assembly that already exist in the current AppDomain. The code below executes all 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 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 embedded resources. Return Assembly.GetExecutingAssembly.LoadEmbeddedAssembly(ResourceName) End Function ''' <summary> ''' Loads an assembly from the current assembly 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 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 

EmbeddedAssemblyResolverClass used to create the actual AssemblyResolve event handler. I added some bells and whistles, adding IDisposable support and events for Initialized and Uninitialized, but you can trim them if you don't want to.

I created the rest of the code in EmbeddedAssemblyResolverModule so that they are global for my assembly, and also because the LoadEmbeddedAssembly method is an extension method that can only be created in modules.

Now all that remains is to create and create an instance of EmbeddedAssemblyResolverClass in any other class of your application that uses the assembly built into its resources.

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

As soon as you call the method from the built-in resource, it will first see if the assembly is already loaded in the current AppDomain if the assembly returns after that. If the embedded assembly has not been loaded, it will be dynamically loaded from the embedded resources, if one exists.

One thing that is good about this code is that it works on assemblies that do not have EntryPoint, such as class libraries. Also, I was successful at loading inline assemblies with inline assemblies that also used this code.

0
source

All Articles