HOWTO: Easily determine if a file is a managed assembly

 

This is a question I’ve seen float around several times. It’s an innocent question that I would think would be easy to find but the continued stream of questions quickly blows that hypothesis. So, I’ll try to increase the I-Gravity of my post by listing some of the various forms of this question.

  1. Is there a way to know if a file is a managed assembly?
  2. How can I tell if a given file is managed code?
  3. How do you verify that a DLL is managed?

If you’ve got your own version of this question please feel free to leave a comment so that other people asking this question can easily acquire it.

Oh, here’s the (managed code) answer:

You can easily check for this by calling Assembly.Load() on a particular file. If that file is a managed assembly this call will succeed. If it fails you’ll get a System.BadImageFormatException.

I-Gravity:  i-grav-i-ty

 

Pronunciation:  ‘I-gra-v&-tE

Function:  noun

Usage:  often attributive

Etymology:  English

Synonym:  google juice

Comments

  • Anonymous
    May 19, 2005
    For a lowest level hint, analyse the PE header:

    // ============================= use at your own risk ===========================
    static bool IsClrImage( string fileName )
    {
    FileStream fs = null;
    try
    {
    fs = new FileStream( fileName, FileMode.Open, FileAccess.Read, FileShare.Read );
    byte[] dat = new byte[300];
    fs.Read( dat, 0, 128 );
    if( (dat[0] != 0x4d) || (dat[1] != 0x5a) ) // "MZ" DOS header
    return false;

    int lfanew = BitConverter.ToInt32( dat, 0x3c );
    fs.Seek( lfanew, SeekOrigin.Begin );
    fs.Read( dat, 0, 24 ); // read signature & PE file header
    if( (dat[0] != 0x50) || (dat[1] != 0x45) ) // "PE" signature
    return false;

    fs.Read( dat, 0, 96 + 128 ); // read PE optional header
    if( (dat[0] != 0x0b) || (dat[1] != 0x01) ) // magic
    return false;

    int clihd = BitConverter.ToInt32( dat, 208 ); // get IMAGE_COR20_HEADER rva-address
    return clihd != 0;
    }
    catch( Exception )
    {
    return false;
    }
    finally
    {
    if( fs != null )
    fs.Close();
    }
    }
    // ==============================================================================

    http://groups.google.de/group/microsoft.public.de.german.entwickler.dotnet.framework/msg/1af0719168e45e84
  • Anonymous
    May 19, 2005
    Junfeng Zhang has a blog post on this as well...looks like I missed that one when I was searching for existing info on this topic:

    http://blogs.msdn.com/junfeng/archive/2004/02/06/68334.aspx
  • Anonymous
    May 19, 2005
    You don't quite explain in what context this information is required. Programmatically or just while browsing and looking at some DLL? Your answer is fine for the former but for the latter I find it much easier to file up ILDASM and drag and drop the DLL onto it. I will give you an error if its not managed. Alternatively you can have the VC++ tool depends.exe associated with the .DLL extension. Then its just a double click to figure it out. A managed DLL will only be linked against mscoree.dll in the dependency list. Its that simple.