如何避免我们的程序因为动态加载的 DLL 有错误而崩溃
我有一个别人写的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的通话中添加 try / catch:
Add try / catch around your call: