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.
Add try / catch around your call: