Hello I am currently trying to resolve an issue regarding a software I am developing.
What I want to achieve: Load a dll into the memory stream from a byte[] without WriteAllBytes (meaning that I want to avoid touching the disk).
I have tried plenty of methods but failed. I think that I successfully did load the byte[] into the memory, but my compiled executable is still looking for it to be loaded from the disk instead of the memory. How do I make it to load it from the memory in order to be able to be utilized?
Let's get into the code.
WebClient client = new WebClient();
string b64strbin = client.DownloadString("URL OF A FILE WHERE IT CONTAINS BYTE[]"); // this will download the string and store it to a variable.
byte[] decode = Convert.FromBase64String(b64strbin); // just decode it from base64 back to byte[]
byte[] packed = QuickLZ.decompress(decode); // decompressed dll byte[] (dont mind about this)
Assembly.Load(packed); // here i am loading the byte[] to the memory but still i get an exception
//methods that require the dll in order to run
Console.Read();
I was finally able to solve the problem by just handling the exception.
All i had to do was just resolve the dll reference manually and load the byte[] with Assembly.Load
.
Solution:
static void Main()
{
try
{
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
//code that depends on the methods of the dll
//we reflect on memory.
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
}
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
{
try
{
WebClient client = new WebClient();
string b64strbin = client.DownloadString("URL OF A FILE WHERE IT CONTAINS BYTE[]");
byte[] decode = Convert.FromBase64String(b64strbin);
return Assembly.Load(QuickLZ.decompress(decode));
}
catch (Exception ex)
{
return null;
}
}
Conclusion: It's really amazing what you can achieve after doing that, i mean that you can use any depended executable file and include the dll without even touching the disk or even including it as embedded resource, by just reflecting the dll into the memory directly with client.DownloadString
. I do believe that sky is the limit after that.