Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Tell if a .NET Assembly Was Compiled as x86, x64 or Any CPU

What's the easiest way to discover (without access to the source project) whether a .NET assembly DLL was compiled as 'x86', 'x64' or 'Any CPU'?

Update: A command-line utility was sufficient to meet my immediate needs, but just for the sake of completeness, if someone wants to tell me how to do it programmatically then that would be of interest too, I'm sure.

like image 602
Tim Long Avatar asked Oct 19 '09 22:10

Tim Long


People also ask

How can I tell if my net assembly is 64-bit?

You might also want to check out this one: check-if-unmanaged-dll-is-32-bit-or-64-bit. In later version of CorFlags, corresponding to . NET 4.5, "32BIT" was replaced by "32BITREQ" and "32BITPREF"..

Which .NET framework should I use x64 or x86?

Application ( exe file) should run on x64 or ia64 or x86 depending on the system. But when you use a library ( dll file) registered as COM it can run even in x86 mode (with x86 applications) on a x64 system.


2 Answers

If you just want to find this out on a given dll, then you can use the CorFlags tool that is part of the Windows SDK:

CorFlags.exe assembly.dll 

If you want to do it using code, take a look at the GetPEKind method of the Module class:

Assembly assembly = Assembly.LoadFrom("path to dll"); PortableExecutableKinds peKind; ImageFileMachine imageFileMachine; assembly.ManifestModule.GetPEKind(out peKind, out imageFileMachine) 

You then need to examine the peKind to check its value. See the MSDN docs for PortableExecutableKinds for more info.

like image 100
adrianbanks Avatar answered Sep 21 '22 12:09

adrianbanks


Thanks Adrian! I've rewritten the snippet in PowerShell so I could use it on the server.

#USAGE #1 # Get-Bitness (dir *.dll | select -first 1) #USAGE #2 # Get-Bitness "C:\vs\projects\bestprojectever\bin\debug\mysweetproj.dll" function Get-Bitness([System.IO.FileInfo]$assemblyFile) {     $peKinds = new-object Reflection.PortableExecutableKinds     $imageFileMachine = new-object Reflection.ImageFileMachine     $a = [Reflection.Assembly]::LoadFile($assemblyFile.Fullname)     $a.ManifestModule.GetPEKind([ref]$peKinds, [ref]$imageFileMachine)      return $peKinds } 
like image 34
Peter Seale Avatar answered Sep 18 '22 12:09

Peter Seale