I want to determine if a native assembly is complied as x64 or x86 from a managed code application (C#).
I think it must be somewhere in the PE header since the OS loader needs to know this information, but I couldn't find it. Of course I prefer to do it in managed code, but if it necessary, I can use native C++.
I rewrote c++ solution in first answer in powershell script. Script can determine this types of .exe and .dll files:
this solution has some advantages over corflags.exe and loading assembly via Assembly.Load in C# - you will never get BadImageFormatException or message about invalid header.
usage example:
you can omit second parameter if you don't need to see details
64-bit binaries are stored in PE32+ format. Try reading http://www.masm32.com/board/index.php?action=dlattach;topic=6687.0;id=3486
There is an easy way to do this with CorFlags. Open the Visual Studio Command Prompt and type "corflags [your assembly]". You'll get something like this:
You're looking at PE and 32BIT specifically.
Any CPU:
PE: PE32
32BIT: 0
x86:
PE: PE32
32BIT: 1
x64:
PE: PE32+
32BIT: 0
Open the dll with a hex editor, like HxD
If the there is a "dt" on the 9th line it is 64bit.
If there is an "L." on the 9th line it is 32bit.
Apparently you can find it in the header of the portable executable. The corflags.exe utility is able to show you whether or not it targets x64. Hopefully this helps you find more information about it.
You can find a C# sample implementation here for the
IMAGE_FILE_HEADER
solution