visual-studio 如何判断 .NET 程序集是编译为 x86、x64 还是任何 CPU

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1591557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:07:10  来源:igfitidea点击:

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

.netvisual-studiox86-64

提问by Tim Long

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'?

发现(无需访问源项目).NET 程序集 DLL 是否编译为“x86”、“x64”或“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.

更新:命令行实用程序足以满足我的直接需求,但为了完整起见,如果有人想告诉我如何以编程方式执行此操作,那么这也很有趣,我敢肯定。

回答by adrianbanks

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

如果您只想在给定的 dll 上找到这一点,那么您可以使用Windows SDK 中的CorFlags工具:

CorFlags.exe assembly.dll


If you want to do it using code, take a look at the GetPEKindmethod of the Moduleclass:

如果你想用代码来做,看看Module类的GetPEKind方法:

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

You then need to examine the peKindto check its value. See the MSDN docsfor PortableExecutableKindsfor more info.

然后,您需要检查peKind以检查其值。请参阅MSDN文档PortableExecutableKinds更多信息。

回答by Peter Seale

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

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

回答by Chris

Thanks Adrian and Peter! Here's a modified version of Peter's Get-Bitness that 1) takes a list of files to examine from the pipeline, and 2) doesn't die it if looks at a non-.NET DLL (e.g. if it looks at certain C++ DLLs):

谢谢阿德里安和彼得!这是 Peter's 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
        }
    }
}

I'm new to PowerShell, so this may not be an example of best practices.

我是 PowerShell 的新手,因此这可能不是最佳实践的示例。

Alternatively, according to https://stackoverflow.com/a/4719567/64257there may also be a handy Get-PEHeader cmdlet in the PowerShell Community Extensions.

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

回答by fiat

C# snippet, based on the Powershell answers:

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