I have a program that needs to discover plugin DLLs on its host.
It does this by enumerating all DLLs within a (fairly large) path. This path includes lots of things, including native DLLs.
foreach (var f in Directory.EnumerateFiles(@"c:\Program Files", "*.dll", SearchOption.AllDirectories))
{
try
{
var assembly = Assembly.LoadFile(f);
var types = assembly.GetTypes();
foreach (var type in types)
{
if (type.GetInterface("My.IInterface") != null)
{
plugins.Add(f);
break;
}
}
assembly = null;
}
catch (Exception e)
{
}
}
If my scanner hits a MS runtime DLL (for example, msvcm80.dll) I get an uncatchable runtime error R6034: "An application has made an attempt to load the C runtime library incorrectly." This window blocks execution of the program. I don't want this DLL (obviously); is there some way to get a graceful error out of this situation?
[Related q: is there an efficient (e.g. non-exception) way of determining if a DLL is a .NET assembly or not, if that DLL is not currently loaded into the process space?]