English 中文(简体)
Detect whether the assembly was built for .NET Compact Framework
原标题:

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

最佳回答

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;
    }
}
问题回答

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

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 FilesMicrosoft Visual Studio 9.0VCinclude) 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.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签