Can I get the processor architecture by loading the dll programmatically in c#?
Is there a class that can do this?
I need to get wether the dll is x86, x64, MSIL etc..
(You can verify the dll is missing from the GAC by opening the GAC c:\windows\assembly and seeing if it is listed.)
Assuming you are only looking at .net assemblies, you can use CorFlags.exe for look at the header of the image.
This blog post explains the usage to determing how to read the results. Excerpt:
Usage: Corflags.exe Assembly [options]
If no options are specified, the flags for the given image are displayed.
...
Here is what each component of the header means:
Version: Contains the version of .NET Redist with which the binary is built.
CLR Header: 2.0 indicates a .Net 1.0 or .Net 1.1 (Everett) image while 2.5 indicates a .Net 2.0 (Whidbey) image.
CorFlags: This is computed by OR’g specific flags to indicate whether the image is ILONLY, its bitness etc. and is used by the loader. ILONLY: Managed images are allowed to contain native code. To be “anycpu” an image shall only contain IL.
32BIT: Even if you have an image that only contains IL it still might have platform dependencies, the 32BIT flag is used to distinguish “x86” images from “anycpu” images. 64-bit images are distinguished by the fact that they have a PE type of PE32+.
The most interesting aspect is the PE and the 32BIT flag of the header. These combine to specify the assembly types. Here is how they would look like for:
- anycpu: PE = PE32 and 32BIT = 0
- x86: PE = PE32 and 32BIT = 1
- 64-bit: PE = PE32+ and 32BIT = 0
Trying to find out by loading the assembly is a chicken-and-egg proposition. If you don't get a BadImageFormatException then the arch is appropriate and you no longer care what it is. If you do get the exception then the program's configuration is wrong. Nothing you can do about that in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With