0

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();

The exception when I try to run. Exception

Community
  • 1
  • 1
0xyg3n
  • 33
  • 10
  • 1
    The assembly you're trying to load references the `BouncyCastle.Crypto` assembly which cannot be found by your app. You have to handle the reference resolution manually. – dymanoid Jun 24 '20 at 09:11
  • Hmm well since i am new in C# could you provide an example or any reference website i can read? – 0xyg3n Jun 24 '20 at 09:39
  • @0xyg3n can you use nuget prackge? – Vivek Nuna Jun 24 '20 at 10:39
  • No i can't the project i am using this is meant for CodeDOM Runtime compile and i don't know how to use a nuget on a runtime compiler. If you do that would solve most of my problems i believe. – 0xyg3n Jun 24 '20 at 10:53
  • http://www.digitallycreated.net/Blog/61/combining-multiple-assemblies-into-a-single-exe-for-a-wpf-application This looks that i got something investigating further.. – 0xyg3n Jun 24 '20 at 10:55
  • Haven't found a solution so far but this also looks really helpful ! https://weblog.west-wind.com/posts/2016/dec/12/loading-net-assemblies-out-of-seperate-folders – 0xyg3n Jun 24 '20 at 11:20

1 Answers1

0

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.

0xyg3n
  • 33
  • 10