首页 文章

如何判断.NET程序集是否编译为x86,x64或任何CPU

提问于
浏览
31

发现(无法访问源项目).NET程序集DLL是否编译为“x86”,“x64”或“任何CPU”的最简单方法是什么?

更新:命令行实用程序足以满足我的直接需求,但仅仅是为了完整性,如果有人想告诉我如何以编程方式执行它,那么这也是有意义的,我敢肯定 .

4 回答

  • 2

    如果您只想在给定的dll上找到它,那么您可以使用属于Windows SDK的CorFlags工具:

    CorFlags.exe assembly.dll
    

    如果您想使用代码执行此操作,请查看Module类的GetPEKind方法:

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

    然后,您需要检查 peKind 以检查其值 . 有关详细信息,请参阅MSDN docs了解 PortableExecutableKinds .

  • 3

    谢谢阿德里安!我已经在PowerShell中重写了代码片段,因此我可以在服务器上使用它 .

    #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
    }
    
  • 13

    C#片段,基于Powershell的答案:

    var modules = assembly.GetModules();
    var kinds = new List<PortableExecutableKinds>();
    var images = new List<ImageFileMachine>();
    foreach (var module in modules)
    {
        PortableExecutableKinds peKinds;
        ImageFileMachine imageFileMachine;
        module.GetPEKind(out peKinds, out imageFileMachine);
    
        kinds.Add(peKinds);
        images.Add(imageFileMachine);
    }
    
    var distinctKinds = kinds.Distinct().ToList();
    var distinctImages = images.Distinct().ToList();
    
  • 46

    谢谢阿德里安和彼得!这是Peter的Get-Bitness的修改版本,1)从管道中获取要检查的文件列表,2)如果查看非.NET DLL(例如,如果它查看某些C DLL),则不会死掉它:

    # example usage: dir *.exe,*.dll | Get-PEKind
    function Get-PEKind {
        Param(
          [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
          [System.IO.FileInfo]$assemblies
        )
    
        Process {
            foreach ($assembly in $assemblies) {
                $peKinds = new-object Reflection.PortableExecutableKinds
                $imageFileMachine = new-object Reflection.ImageFileMachine
                try
                {
                    $a = [Reflection.Assembly]::LoadFile($assembly.Fullname)
                    $a.ManifestModule.GetPEKind([ref]$peKinds, [ref]$imageFileMachine)
                }
                catch [System.BadImageFormatException]
                {
                    $peKinds = [System.Reflection.PortableExecutableKinds]"NotAPortableExecutableImage"
                }
    
                $o = New-Object System.Object
                $o | Add-Member -type NoteProperty -name File -value $assembly
                $o | Add-Member -type NoteProperty -name PEKind -value $peKinds
                Write-Output $o
            }
        }
    }
    

    我是PowerShell的新手,所以这可能不是最佳实践的一个例子 .

    或者,根据https://stackoverflow.com/a/4719567/64257PowerShell Community Extensions中可能还有一个方便的Get-PEHeader cmdlet .

相关问题