如何避免我们的程序因为动态加载的 DLL 有错误而崩溃

发布于 2025-01-08 18:25:56 字数 2394 浏览 1 评论 0原文

我有一个别人写的dll,它有很多缺陷。假设该 DLL 中只定义了一个类。我需要导入该 DLL 并创建该类的实例。 DLL代码可能如下:

   [Serializable()]
    public class makeExceptionClass
    {
        public bool isStringNormalized(string aString)
        {
            // A null check should be performed
            return aString.IsNormalized();
        }
    }

我编写了一个小程序来检查即使DLL崩溃我的程序是否仍然可以运行。该程序只是一个概念证明。它有两个参数,第一个用于从程序集中直接加载 DLL,第二个用于引发崩溃。 该程序的代码如下:

class Program
{
    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    static void Main(string[] args)
    {
        string typeOfLoading = args[0];
        string crash = args[1];

        // Load the DLL
        if (typeOfLoading.Equals("direct") == true)
        {
            Console.WriteLine("Loading directly a DLL");
            Assembly anAssembly = Assembly.Load("unloadableDLL");    // Directly load the DLL
            unloadableDLL.makeExceptionClass anObject = (unloadableDLL.makeExceptionClass)anAssembly.CreateInstance("unloadableDLL.makeExceptionClass");

            if (crash.Equals("crash") == true)
            {
                bool test = anObject.isStringNormalized(null);
            }
            else
            {
                bool test = anObject.isStringNormalized("test");
            }
        }
        else if (typeOfLoading.Equals("indirect") == true)
        {
            Console.WriteLine("Loading indirectly a DLL");
            AppDomain anAppDomain = AppDomain.CreateDomain("RemoteLoaderDomain");   // Assume it does not fail;
            Type t = typeof(unloadableDLL.makeExceptionClass);
            unloadableDLL.makeExceptionClass anObject = (unloadableDLL.makeExceptionClass)anAppDomain.CreateInstanceAndUnwrap("unloadableDLL", t.FullName);

            if (crash.Equals("crash") == true)
            {
                bool test = anObject.isStringNormalized(null);
            }
            else
            {
                bool test = anObject.isStringNormalized("test");
            }
            Console.WriteLine("Unloading the domain");
            AppDomain.Unload(anAppDomain);                
        }
        else
        {
            // don't care
        }

        // terminate
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
}

问题是,无论 dll 是直接加载还是加载到 AppDomain 中,我的程序都会崩溃。 我对 C# 完全陌生(今天早上开始),但我有 C++ 背景。

I have a dll written by someone else and that has many flaws. Let's assume there is only one class defined in this DLL. I need to import this DLL and create an instance of this class.
The DLL code might be the following :

   [Serializable()]
    public class makeExceptionClass
    {
        public bool isStringNormalized(string aString)
        {
            // A null check should be performed
            return aString.IsNormalized();
        }
    }

I wrote a small program to check whether or not my program can still run even if the dll crashes. The program is just a proof of concept. It takes two arguments, the first is used to load directly the DLL from an assembly and the second is used to provoke a crash.
The code of the program is the following :

class Program
{
    [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
    static void Main(string[] args)
    {
        string typeOfLoading = args[0];
        string crash = args[1];

        // Load the DLL
        if (typeOfLoading.Equals("direct") == true)
        {
            Console.WriteLine("Loading directly a DLL");
            Assembly anAssembly = Assembly.Load("unloadableDLL");    // Directly load the DLL
            unloadableDLL.makeExceptionClass anObject = (unloadableDLL.makeExceptionClass)anAssembly.CreateInstance("unloadableDLL.makeExceptionClass");

            if (crash.Equals("crash") == true)
            {
                bool test = anObject.isStringNormalized(null);
            }
            else
            {
                bool test = anObject.isStringNormalized("test");
            }
        }
        else if (typeOfLoading.Equals("indirect") == true)
        {
            Console.WriteLine("Loading indirectly a DLL");
            AppDomain anAppDomain = AppDomain.CreateDomain("RemoteLoaderDomain");   // Assume it does not fail;
            Type t = typeof(unloadableDLL.makeExceptionClass);
            unloadableDLL.makeExceptionClass anObject = (unloadableDLL.makeExceptionClass)anAppDomain.CreateInstanceAndUnwrap("unloadableDLL", t.FullName);

            if (crash.Equals("crash") == true)
            {
                bool test = anObject.isStringNormalized(null);
            }
            else
            {
                bool test = anObject.isStringNormalized("test");
            }
            Console.WriteLine("Unloading the domain");
            AppDomain.Unload(anAppDomain);                
        }
        else
        {
            // don't care
        }

        // terminate
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
}

The problem is that my program crashes regardless if the dll is loaded directly or into an AppDomain.
I am completely new to C# (I started this morning) but I have a C++ background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[浮城] 2025-01-15 18:25:56

在您的通话中添加 try / catch:

  try
  {
    if (crash.Equals("crash") == true)
    {
      bool test = anObject.isStringNormalized(null);
    }
    else
    {
      bool test = anObject.isStringNormalized("test");
    }
  } catch (Exception ex) {
    Console.WriteLine("exception in dll call: "+ex);
  } 

Add try / catch around your call:

  try
  {
    if (crash.Equals("crash") == true)
    {
      bool test = anObject.isStringNormalized(null);
    }
    else
    {
      bool test = anObject.isStringNormalized("test");
    }
  } catch (Exception ex) {
    Console.WriteLine("exception in dll call: "+ex);
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文