|
-
Feb 25th, 2011, 07:17 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Embedding DLL resources into Executable (No more ILmerge)
Hello.
I've used ILMerge in the past when I needed to include the DLL's my assemblies depended on and only have to distribute a single exe file.
Jeffrey Richter recently posted some c# code that allows you to embed the DLL's and load them from a byte array as embedded resources.
I wonder if anyone here has done this in VB.net yet?( Without a bunch of off topic discussion about "if this is legal to do")
I've tried to understand how you would do this in VB.net, but I'm kinda at a loss..
Here is the original link
http://blogs.msdn.com/b/microsoft_pr...d-edition.aspx
Here's the code sample he gives(in c#), I'm just not sure how to handle the event in VB.net since among many things the sub main is semi hidden and my lack of the appdomain knowledge
Code:
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
String resourceName = "AssemblyLoadingAndReflection." +
new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
A rough conversion to vb.net
Code:
AppDomain.CurrentDomain.AssemblyResolve += Function(sender, args)
Dim resourceName As [String] = "AssemblyLoadingAndReflection." + New AssemblyName(args.Name).Name & ".dll"
Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
Dim assemblyData As [Byte]() = New [Byte](stream.Length - 1) {}
stream.Read(assemblyData, 0, assemblyData.Length)
Return Assembly.Load(assemblyData)
End Using
End Function
Anyone ever done this before? Perhaps a working code sample?
Thanks in advance!
-
Feb 25th, 2011, 08:26 PM
#2
Re: Embedding DLL resources into Executable (No more ILmerge)
Never done this but here is how I converted to VB.NET, have not tried it.
Code:
Option Strict On
Imports System.Reflection
Code:
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf test
Code:
Private Function test( _
ByVal sender As Object, _
ByVal args As System.ResolveEventArgs) As System.Reflection.Assembly
Dim resourceName As String = "AssemblyLoadingAndReflection." & _
New AssemblyName(args.Name).Name & ".dll"
Using stream = System.Reflection.Assembly.GetExecutingAssembly() _
.GetManifestResourceStream(resourceName)
Dim assemblyData(CInt(stream.Length - 1)) As Byte
stream.Read(assemblyData, 0, assemblyData.Length)
Return System.Reflection.Assembly.Load(assemblyData)
End Using
End Function
-
Feb 25th, 2011, 10:09 PM
#3
Thread Starter
Addicted Member
Re: Embedding DLL resources into Executable (No more ILmerge)
Ok, just tested it out. Got it working after a little trial and error.
Here was the results.
I placed all the code in the ApplicationEvents.vb
Code:
Private Sub Form1_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf LoadDLLFromStream
End Sub
Private Function LoadDLLFromStream( _
ByVal sender As Object, _
ByVal args As System.ResolveEventArgs) As System.Reflection.Assembly
Dim resourceName As String = "YourAssemblyName_CaseSensitive." & New AssemblyName(args.Name).Name & ".dll"
'had to use this line to debug and figure out why it didnt load at first
Dim resources() As String = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames
Using stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
Dim assemblyData(CInt(stream.Length - 1)) As Byte
stream.Read(assemblyData, 0, assemblyData.Length)
Return System.Reflection.Assembly.Load(assemblyData)
End Using
End Function
Worked perfectly after I figured out what the resource name really was...
Thanks for the help.
-
Feb 25th, 2011, 11:26 PM
#4
Re: [RESOLVED] Embedding DLL resources into Executable (No more ILmerge)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|