.NETアセンブリDLLが「x86」、「x64」、または「任意のCPU」としてコンパイルされているかどうかを(ソースプロジェクトにアクセスせずに)検出する最も簡単な方法は何ですか?
更新:コマンドラインユーティリティは私の当面のニーズを満たすのに十分でしたが、完全を期すために、誰かがプログラムでそれを行う方法を教えてくれれば、それも興味深いでしょう。
.NETアセンブリDLLが「x86」、「x64」、または「任意のCPU」としてコンパイルされているかどうかを(ソースプロジェクトにアクセスせずに)検出する最も簡単な方法は何ですか?
更新:コマンドラインユーティリティは私の当面のニーズを満たすのに十分でしたが、完全を期すために、誰かがプログラムでそれを行う方法を教えてくれれば、それも興味深いでしょう。
特定のdllでこれを確認したいだけの場合は、WindowsSDKの一部である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ドキュメントを参照しPortableExecutableKinds
てください。
エイドリアンありがとう!サーバーで使用できるように、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
}
エイドリアンとピーターに感謝します!これは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/64257によると、 PowerShellCommunityExtensionsに便利なGet-PEHeaderコマンドレットがある場合もあります。
Powershellの回答に基づくC#スニペット:
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();