views:

65

answers:

3

Hi, I am using LoadFrom(), to load dlls, but for some reason this load function doesn't work on all dlls, i want to load 3000 dlls to get from each one the copyright attribute.

my code :

    class ReverseDLL
{
    private Assembly assembly;
    private AssemblyDescriptionAttribute desc;
    private AssemblyTitleAttribute title;
    private AssemblyCopyrightAttribute copyRight;

    public string getCopyright(string path)
    {
        try
        {
            //assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes(path));
            assembly = System.Reflection.Assembly.LoadFrom(path);//"C:\\Windows\\winsxs\\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\\msvcm90d.dll");//path);// LoadFrom(path);

                desc = (AssemblyDescriptionAttribute)
                AssemblyDescriptionAttribute.GetCustomAttribute(
                assembly, typeof(AssemblyDescriptionAttribute));

                title = (AssemblyTitleAttribute)
                AssemblyTitleAttribute.GetCustomAttribute(
                assembly, typeof(AssemblyTitleAttribute));

                copyRight = (AssemblyCopyrightAttribute)AssemblyCopyrightAttribute.GetCustomAttribute(assembly, typeof(AssemblyCopyrightAttribute));
        }
        catch
        {
              this.copyRight = new AssemblyCopyrightAttribute("");
        }

        if (this.copyRight == null)
            this.copyRight = new AssemblyCopyrightAttribute("");

        return copyRight.Copyright;
    }
}
+3  A: 

I don't know about the reflection problem without you providing more info (such as the error), but you could also try access the file itself:

string copyright = FileVersionInfo.GetVersionInfo(path).LegalCopyright;

This accesses the file-system meta-data (like you would see in explorer), and has the advantage of working for both managed and unmanaged dlls; but it requires that meta-data to exist (it doesn't look at the attribute).

Edit: a quick check indicates that (as expected) the compiler does check for this attribute and populate the file meta-data correctly.

Marc Gravell
Thanks a lot for this suggestion, it's really helped a lot,can i get also the digital signature from files?
kimo
@kimo - I honestly don't know.
Marc Gravell
A: 

Have you tried stopping on exceptions? Ctrl + Alt + E, stop on framework exceptions when they are thrown. The exception message should give you some information as to why the DLL couldn't be loaded.

Will A
A: 

Using reflection is not the optimal approach, as some of the dll may have dependencies you don't have.

Using a metadata parser can give you the things you want,

http://ccimetadata.codeplex.com/

http://www.mono-project.com/Cecil

The way Marc mentioned does not work for most .NET specific metadata.

Lex Li