【问题标题】:How to assembly.load(byte[]) managed dll to memory, without touching the disk如何在不接触磁盘的情况下将 assembly.load(byte[]) 托管的 dll 加载到内存中
【发布时间】:2020-06-24 09:06:57
【问题描述】:

您好,我目前正在尝试解决有关我正在开发的软件的问题。

我想要实现的目标:从没有 WriteAllBytes 的 byte[] 将 dll 加载到内存流中(这意味着我想避免接触磁盘)。

我尝试了很多方法,但都失败了。我认为我成功地将 byte[] 加载到内存中,但是我编译的可执行文件仍在寻找从磁盘而不是内存加载它。如何让它从内存中加载以便能够使用?

让我们进入代码。

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

我尝试运行时的异常。

【问题讨论】:

  • 您尝试加载的程序集引用了您的应用找不到的 BouncyCastle.Crypto 程序集。您必须手动处理参考分辨率。
  • 嗯,因为我是 C# 新手,你能提供一个例子或任何我可以阅读的参考网站吗?
  • @0xyg3n 你能用 nuget prackge 吗?
  • 不,我不能将我正在使用的项目用于 CodeDOM 运行时编译,我不知道如何在运行时编译器上使用 nuget。如果你这样做,我相信会解决我的大部分问题。
  • digitallycreated.net/Blog/61/… 这看起来我得到了一些进一步调查..

标签: c# memory dll managed assembly.load


【解决方案1】:

我终于能够通过处理异常来解决问题。 我所要做的就是手动解析 dll 引用并使用 Assembly.Load 加载 byte[]。

解决方案:

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;
            }
        }

结论: 这样做之后你能取得的成就真是太棒了,我的意思是你可以使用任何依赖的可执行文件并包含 dll,甚至无需接触磁盘甚至将其包含为嵌入式资源,只需使用 client.DownloadString 直接将 dll 反映到内存中。 我相信在那之后天空才是极限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    相关资源
    最近更新 更多