I've got an arbitrary list of .NET assemblies.
I need to programmatically check if each DLL was built for x86 (as opposed to x64 or Any CPU). Is this possible?
I've got an arbitrary list of .NET assemblies.
I need to programmatically check if each DLL was built for x86 (as opposed to x64 or Any CPU). Is this possible?
How about you just write you own? The core of the PE architecture hasn't been seriously changed since its implementation in Windows 95. Here's a C# example:
Now the current constants are:
But with this method it allows for the possibilities of new constants, just validate the return as you see fit.
Look at
System.Reflection.AssemblyName.GetAssemblyName(string assemblyFile)
You can examine assembly metadata from the returned AssemblyName instance:
Using PowerShell:
Here, ProcessorArchitecture identifies target platform.
I'm using PowerShell in this example to call the method.
More generic way - use file structure to determine bitness and image type:
Compilation mode enumeration
Source code with explanation at GitHub
You can use the CorFlags CLI tool (for instance, C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\CorFlags.exe) to determine the status of an assembly, based on its output and opening an assembly as a binary asset you should be able to determine where you need to seek to determine if the 32BIT flag is set to 1 (x86) or 0 (Any CPU or x64, depending on
PE
):The blog post x64 Development with .NET has some information about
corflags
.Even better, you can use
Module.GetPEKind
to determine whether an assembly isPortableExecutableKinds
valuePE32Plus
(64-bit),Required32Bit
(32-bit and WOW), orILOnly
(any CPU) along with other attributes.cfeduke notes the possibility of calling GetPEKind. It's potentially interesting to do this from PowerShell.
Here, for example, is code for a cmdlet that could be used: https://stackoverflow.com/a/16181743/64257
Alternatively, at https://stackoverflow.com/a/4719567/64257 it is noted that "there's also the Get-PEHeader cmdlet in the PowerShell Community Extensions that can be used to test for executable images."
Try to use CorFlagsReader from this project at CodePlex. It has no references to other assemblies and it can be used as is.