Last active
April 20, 2022 12:44
-
-
Save f2face/8972308 to your computer and use it in GitHub Desktop.
[VB.NET] Embed DLL Reference in EXE as Embedded Resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Embed DLL reference into single EXE / Use DLL as embedded resource | |
' Menggabungkan DLL reference ke dalam EXE sebagai embedded resource, tanpa ILMerge ataupun software merger lainnya. | |
' Code by TLS (http://stackoverflow.com/a/9678903) | |
' Persiapan (gambar): | |
' 1. https://drive.google.com/file/d/0B_JLt2gqN3MPWTcwcWtEbXBLRnM/edit?usp=sharing | |
' 2. https://drive.google.com/file/d/0B_JLt2gqN3MPRS1GbGRHaFJLaDA/edit?usp=sharing | |
'========================================================================================== | |
' 1. Buat Module baru, masukkan kode : | |
' Ganti [YOUR_NAMESPACE] dengan Namespace project ente | |
Imports System.IO | |
Imports System.Reflection | |
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("[YOUR_NAMESPACE].{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 | |
'========================================================================================== | |
' 2. Di startup form, tambahkan kode : | |
Sub New() | |
EnsureInitialized() | |
InitializeComponent() | |
End Sub | |
'------------------------------------------------------------------------------------------ | |
' That's it! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can also add this line to load the assembly name dynamically
Dim resourceFullName As String = String.Format("{0}.{1}.dll", Assembly.GetExecutingAssembly().GetName().Name, e.Name.Split(","c)(0))