Determining the Bitness (x86 or x64) of a DLL
Wondering if that DLL is x86 or X64? Here's how to find out.
Join the DZone community and get the full member experience.
Join For Freehello guys and gals!
in this article, we’re going to tackle a pretty frustrating scenario: making sure all your .net assemblies or even native dlls target the same architecture. at the time of writing, applications tend to be targeted at x86 or x64 architectures, or both. however, mixing dlls or executables compiled for different architectures is a recipe for disaster . this kind of disaster, in fact:
that’s a badimageformatexception, saying:
an unhandled exception of type ‘system.
badimageformatexception
‘ occurred in microsoft.visualstudio.hostingprocess.utilities.dll
additional information: could not load file or assembly ‘classlibrary, version=1.0.0.0, culture=neutral, publickeytoken=null’ or one of its dependencies.
an attempt was made to load a program with an incorrect format.
this is why it’s happening:
that’s a very, very stupid thing to do, having an x64 console application depend on an x86 class library (or dll). remember:
great, now you know. but you’re bound to run into the badimageformatexception problem anyway, because while it’s easy to control the bitness of the assemblies in your project, you often depend on dlls provided by third parties, and their own bitness isn’t clearly defined. unfortunately, you can’t rely on rightclick -> properties -> details because that doesn’t give you the bitness at all.
fortunately, however, there are tools you can use to find out what architecture those dlls are compiled for.
let’s start off with .net assemblies. if the dll in question is a .net assembly, you can use the corflags utility to find out the bitness . corflags is part of the visual studio developer tools, so you’ll first need to start a visual studio developer command prompt (see this stackoverflow thread or search windows for corflags.exe in case you can’t find one from the start menu).
once you’ve located corflags.exe or have a visual studio developer command prompt, run corflags.exe with the full path to the dll you want to inspect, and check the 32bit setting in the output:
now corflags.exe works great to find out the architecture of a .net assembly, but can’t tell you the architecture of a native dll. this is what happens if you try to use it with sdl2.dll, for instance:
for unmanaged/native dlls, we can instead use dumpbin . this will spit out a bunch of information, so you can find the architecture quickly if you filter the information by including only the line containing the word “machine”, as the linked stack overflow answer suggests:
so there you go – this article has presented two different ways to check the bitness or cpu architecture of managed and unmanaged dlls respectively, which helps when troubleshooting those dreaded badimageformatexceptions. in reality this article is a consolidation of the information in the two stack overflow threads linked above, and i would like to thank the authors because i have found their tips useful on many occasions.
this article was originally posted here at programmer’s ranch on 28th september 2014.
Published at DZone with permission of Daniel D'agostino, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments