How can I determine if a .NET assembly was built for x86 or x64?

后端 未结 15 879
面向向阳花
面向向阳花 2020-11-22 09:08

I\'ve got an arbitrary list of .NET assemblies.

I need to programmatically check if each DLL was built for x86 (as opposed to x64 or Any CPU). Is this possible?

相关标签:
15条回答
  • 2020-11-22 09:46

    I've cloned a super handy tool that adds a context menu entry for assemblies in the windows explorer to show all available info:

    Download here: https://github.com/tebjan/AssemblyInformation/releases

    0 讨论(0)
  • 2020-11-22 09:47
    [TestMethod]
    public void EnsureKWLLibrariesAreAll64Bit()
    {
        var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies().Where(x => x.FullName.StartsWith("YourCommonProjectName")).ToArray();
        foreach (var assembly in assemblies)
        {
            var myAssemblyName = AssemblyName.GetAssemblyName(assembly.FullName.Split(',')[0] + ".dll");
            Assert.AreEqual(ProcessorArchitecture.MSIL, myAssemblyName.ProcessorArchitecture);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:49

    You can use the CorFlags CLI tool (for instance, C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\CorFlags.exe) to determine the status of an assembly, based on its output and opening an assembly as a binary asset you should be able to determine where you need to seek to determine if the 32BIT flag is set to 1 (x86) or 0 (Any CPU or x64, depending on PE):

    Option    | PE    | 32BIT
    ----------|-------|---------
    x86       | PE32  | 1
    Any CPU   | PE32  | 0
    x64       | PE32+ | 0
    

    The blog post x64 Development with .NET has some information about corflags.

    Even better, you can use Module.GetPEKind to determine whether an assembly is PortableExecutableKinds value PE32Plus (64-bit), Required32Bit (32-bit and WOW), or ILOnly (any CPU) along with other attributes.

    0 讨论(0)
提交回复
热议问题