Question

Having a .NET assembly, how can I detect whether it was built for .NET CF or a full framework?

Was it helpful?

Solution

It's quite simple:

public enum AssemblyType
{
    CompactFramework,
    FullFramework,
    NativeBinary
}

public AssemblyType GetAssemblyType(string pathToAssembly)
{
    try
    {
        Assembly asm = Assembly.LoadFrom(pathToAssembly);
        var mscorlib = asm.GetReferencedAssemblies().FirstOrDefault(a => string.Compare(a.Name, "mscorlib", true) == 0);
        ulong token = BitConverter.ToUInt64(mscorlib.GetPublicKeyToken(), 0);

        switch (token)
        {
            case 0xac22333d05b89d96:
                return AssemblyType.CompactFramework;
            case 0x89e03419565c7ab7:
                return AssemblyType.FullFramework;
            default:
                throw new NotSupportedException();
        }
    }
    catch (BadImageFormatException)
    {
        return AssemblyType.NativeBinary;
    }
}

OTHER TIPS

The best bet would be to grab the C's include file header called winnt.h, found in your standard VS Professional (usually C:\Program Files\Microsoft Visual Studio 9.0\VC\include) and from there, load the .EXE into a PE Dumper of some sort, or use a Hex Dumper. 1. Look at the DOS HEader from offset 0x0. 2. The NT Header would immediately follow after the DOS header. 3. The Machine ID is what you are looking for. The machine ID for CF (ARM/MIPS) would be 0x010C/0x0169, respectively. If you wish to invest more time in poking around.. read on, 4. Then you have the Data directory immediately following after NT Header. It is the 15th data directory entry is the indication of whether the .EXE is .NET or not. If it is 0, then it is a native .EXE.

Combined together you can then tell if the executable is .NET and for the CF.

Look here for more details.

Hope this helps, Best regards, Tom.

I rather use CCI or Cecil to parse its metadata and check out which set of references it depends on.

http://ccimetadata.codeplex.com/

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top