Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the processor architecture of an assembly dll? [duplicate]

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..

like image 224
3ggerhappy Avatar asked Nov 15 '10 15:11

3ggerhappy


People also ask

How do I check if a DLL is in the GAC?

(You can verify the dll is missing from the GAC by opening the GAC c:\windows\assembly and seeing if it is listed.)


2 Answers

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
like image 189
Mike Ohlsen Avatar answered Oct 03 '22 23:10

Mike Ohlsen


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.

like image 30
Hans Passant Avatar answered Oct 03 '22 23:10

Hans Passant