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

时间:2009-10-19 22:47:42

标签: .net visual-studio x86-64

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

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

4 个答案:

答案 0 :(得分:47)

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

CorFlags.exe assembly.dll

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

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

然后,您需要检查peKind以检查其值。有关详情,请参阅PortableExecutableKinds {{1}}。

答案 1 :(得分:13)

感谢阿德里安!我在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
}

答案 2 :(得分:3)

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();

答案 3 :(得分:2)

感谢阿德里安和彼得!这是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。

相关问题