Determining if a .NET assembly is built for “Any CPU”, 32-bit or 64-bit using the CorFlags utility
Posted: (EET/GMT+2)
Sometimes, you might run into a situation where you have a set of .NET built assemblies (EXE or DLL files), and you must determine whether they are built for "32-bit" (x86), "64-bit" (x64) or "Any CPU".
You could do this with tools like ILDASM or DotPeek, but you can also use the CorFlags utility. This utility is part of the Windows SDK, and is usually in the path similar to the following:
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\ bin\NETFX 4.5.1 Tools\CorFlags.exe
Now, CorFlags is originally a conversion tool, but it can also display useful information about an assembly. To run the tool, open up the Visual Studio developer command prompt, and run "corflags YourAssembly.dll". This will output useful information that you can use to track the "bitness" of the said file.
Here's the CorFlags output for an x64 assembly:
Version : v4.0.30319 CLR Header: 2.5 PE : PE32+ CorFlags : 0x1 ILONLY : 1 32BITREQ : 0 32BITPREF : 0 Signed : 0
Here, the field "PE" is "PE32+", which means the assembly has been built for x64 (64-bit). Compare this to a build for x86 (32-bit). Here's the output:
Version : v4.0.30319 CLR Header: 2.5 PE : PE32 CorFlags : 0x3 ILONLY : 1 32BITREQ : 1 32BITPREF : 0 Signed : 0
In this listing, the important thing to note is the "32BITREQ" field. This, when set to "1", indicates that the build is not 32-bit only.
Finally, here's the CorFlags output for an "Any CPU" assembly:
Version : v4.0.30319 CLR Header: 2.5 PE : PE32 CorFlags : 0x9 ILONLY : 1 32BITREQ : 0 32BITPREF : 0 Signed : 0
Here, the "32BITREQ" field is zero, and thus, the assembly can run as a 64-bit or a 32-bit process. As a summary:
x64 build: PE = PE32+ x86 build: PE = PE32 and 32BITREQ = 1 Any CPU build: PE = PE32 and 32BITREQ = 0
For more details about the CorFlags too, see the documentation.