Author Topic: Embed managed dll files into the app's executable file - VB & C#  (Read 3899 times)

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
All of the related code can be found here:

     https://www.advancedhmi.com/forum/index.php?topic=2567.0

The idea behind this is to create a standalone executable file, which can be placed even on the desktop, without thinking of where all the required dll files are. Not recommended for AdvancedHMI solution since you can break something.

This idea could be described with these steps:

1) Add dll files as existing items to the project's Resources under Files (see the attached picture as an example of how to get there)
2) Add a reference to each of these dll files now located in the "Resources" folder
3) For each of these newly created references, set the Copy Local property to False
4) Resolve and load these assemblies with the handler added to the ApplicationEvents.vb file for VB and to the Program.cs file for C#

Step 3) is kind of optional since it doesn't affect the resulting exe file being standalone (if the Copy Local is set to True, it will only copy those dll files to the Debug folder but the exe file can be moved or copied to a different location all by itself).

This works but there might be a better way of doing this.

The example code, from the link mentioned above, has 3 dll files handled this way - Modbus.dll, log4net.dll and Unme.Common.dll.

« Last Edit: June 05, 2020, 12:54:35 AM by Godra »

bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Embed managed dll files into the app's executable file - VB & C#
« Reply #1 on: June 05, 2020, 06:56:42 PM »
If starting a new project
Right Click Project Properties - Application - View Application Events

Code: [Select]
Namespace My
    ' The following events are available for MyApplication:
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
        Private Sub AppStart(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAssemblies
        End Sub

        Private Function ResolveAssemblies(ByVal sender As Object, ByVal e As System.ResolveEventArgs) As Reflection.Assembly
            Dim desiredAssembly = New Reflection.AssemblyName(e.Name)

            If desiredAssembly.Name = "EPPlus" Then Return Reflection.Assembly.Load(My.Resources.EPPlus)
            'If desiredAssembly.Name = "log4net" Then Return Reflection.Assembly.Load(My.Resources.log4net)
            'If desiredAssembly.Name = "Unme.Common" Then Return Reflection.Assembly.Load(My.Resources.Unme_Common)

            Return Nothing
        End Function
    End Class
End Namespace
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================