Detect whether the assembly was built for .NET Compact Framework

强颜欢笑 提交于 2019-12-01 07:05:03

问题


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


回答1:


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;
    }
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/1817152/detect-whether-the-assembly-was-built-for-net-compact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!