Archive for November, 2016

Check whether a .NET dll is built for Any CPU, x86, or x64

November 16, 2016 Leave a comment

As much as I would like all builds to come from a build server, many times a build comes from a developer’s machine. When I receive a build from another developer’s machine, I need to check if their build is targeting the correct platform.

I can use CorFlags.exe, which is part of the .NET Framework SDK, to find out this information from a dll. Running CorFlags.exe with the file path to the dll will produce the following output:

>> CorFlags "C:\example.dll"

Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version
Copyright (c) Microsoft Corporation.  All rights reserved.

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 0x3
ILONLY    : 1
32BITREQ  : 1
Signed    : 0

The two fields we need to look at are “PE” and “32BITREQ”.

Any CPU PE: PE32, 32BITREQ: 0
x86 PE: PE32, 32BITREQ: 1
x64 PE: PE32+, 32BITREQ: 0

To programmatically determine the target platform, we can use Module.GetPEKind().

Assembly a = Assembly.ReflectionOnlyLoadFrom(@"C:\example.dll");

PortableExecutableKinds peKind;
ImageFileMachine machine;

a.ManifestModule.GetPEKind(out peKind, out machine);


The results of peKind can be interpreted with:

Any CPU ILOnly
x86 ILOnly, Required32Bit
x64 ILOnly, PE32Plus