.NETアセンブリの任意のリストを持っています。
各DLLが(x64またはAny CPUではなく)x86用にビルドされたかどうかをプログラムで確認する必要があります。これは可能ですか?
.NETアセンブリの任意のリストを持っています。
各DLLが(x64またはAny CPUではなく)x86用にビルドされたかどうかをプログラムで確認する必要があります。これは可能ですか?
回答:
見る System.Reflection.AssemblyName.GetAssemblyName(string assemblyFile)
返されたAssemblyNameインスタンスからのアセンブリメタデータを調べることができます。
PowerShellの使用:
[36] C:\> [reflection.assemblyname] :: GetAssemblyName( "$ {pwd} \ Microsoft.GLEE.dll")| fl
名前:Microsoft.GLEE
バージョン:1.0.0.0
CultureInfo:
CodeBase:file:/// C:/ projects / powershell / BuildAnalyzer / ...
EscapedCodeBase:file:/// C:/ projects / powershell / BuildAnalyzer / ...
プロセッサアーキテクチャ:MSIL
フラグ:PublicKey
HashAlgorithm:SHA1
VersionCompatibility:SameMachine
KeyPair:
FullName:Microsoft.GLEE、Version = 1.0.0.0、Culture = neut ... 
ここで、ProcessorArchitectureはターゲットプラットフォームを識別します。
この例ではPowerShellを使用してメソッドを呼び出しています。
[reflection.assemblyname]::GetAssemblyName("${pwd}\name.dll")プロセスの現在のディレクトリが現在のプロバイダーと同じではない場合があるので試してみてください(ここにDLLがあると思います)
                    // DevDiv 216459: This code originally used Assembly.GetName(), but that requires FileIOPermission, which isn't granted in medium trust. However, Assembly.FullName *is* accessible in medium trust.残念ながら、GetName instance method; を使用せずにProcessorArchitectureを読み取る方法はありません。を使用するAssemblyName constructorと、フィールドは常にに設定されNoneます。
                    あなたは使用することができますCorFlags  CLIのアセンブリの状態を判断するために、その出力に基づいて、Aからアセンブリを開く:ツール(の\ Program Files \ MicrosoftのSDK \ Windowsの\ v7.0の\ビン\ CorFlags.exeを例えば、C)を32BITフラグが1(x86)または0(すべてのCPUまたはx64に応じてPE)に設定されているかどうかを判断するために探す必要がある場所を判断できるバイナリアセット:
Option    | PE    | 32BIT
----------|-------|---------
x86       | PE32  | 1
Any CPU   | PE32  | 0
x64       | PE32+ | 0
ブログ投稿x64 Development with .NETには、に関するいくつかの情報がありcorflagsます。
さらに良いことに、他の属性と共に、アセンブリが値(64ビット)、(32ビットおよびWOW)、または(任意のCPU)であるかどうかを判断するために使用Module.GetPEKindできます。PortableExecutableKindsPE32PlusRequired32BitILOnly
明確にするために、CorFlags.exeは.NET Framework SDKの一部です。私のマシンには開発ツールがあり、DLLが32ビットのみかどうかを判断する最も簡単な方法は次のとおりです。
Visual Studioコマンドプロンプトを開きます(Windowsの場合:[スタート] / [プログラム] / [Microsoft Visual Studio] / [Visual Studioツール] / [Visual Studio 2008コマンドプロンプト])。
問題のDLLを含むディレクトリへのCD
次のようにcorflagsを実行します。
corflags MyAssembly.dll
次のような出力が得られます。
Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  3.5.21022.8
Copyright (c) Microsoft Corporation.  All rights reserved.
Version   : v2.0.50727
CLR Header: 2.5
PE        : PE32
CorFlags  : 3
ILONLY    : 1
32BIT     : 1
Signed    : 0
コメントに従って、上記のフラグは次のように読む必要があります:
32BITREQなりました。32BITPREF32BIT
                    自分で書いてみませんか?PEアーキテクチャのコアは、Windows 95での実装以降、大幅に変更されていません。C#の例を次に示します。
    public static ushort GetPEArchitecture(string pFilePath)
    {
        ushort architecture = 0;
        try
        {
            using (System.IO.FileStream fStream = new System.IO.FileStream(pFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                using (System.IO.BinaryReader bReader = new System.IO.BinaryReader(fStream))
                {
                    if (bReader.ReadUInt16() == 23117) //check the MZ signature
                    {
                        fStream.Seek(0x3A, System.IO.SeekOrigin.Current); //seek to e_lfanew.
                        fStream.Seek(bReader.ReadUInt32(), System.IO.SeekOrigin.Begin); //seek to the start of the NT header.
                        if (bReader.ReadUInt32() == 17744) //check the PE\0\0 signature.
                        {
                            fStream.Seek(20, System.IO.SeekOrigin.Current); //seek past the file header,
                            architecture = bReader.ReadUInt16(); //read the magic number of the optional header.
                        }
                    }
                }
            }
        }
        catch (Exception) { /* TODO: Any exception handling you want to do, personally I just take 0 as a sign of failure */}
        //if architecture returns 0, there has been an error.
        return architecture;
    }
}
現在の定数は次のとおりです。
0x10B - PE32  format.
0x20B - PE32+ format.
ただし、このメソッドを使用すると、新しい定数の可能性が可能になります。必要に応じて戻り値を検証してください。
CodePlexでこのプロジェクトの CorFlagsReaderを使用してみてください。他のアセンブリへの参照はなく、そのまま使用できます。
[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);
    }
}以下は、実行するバッチファイルでcorflags.exeすべてに対してdlls及びexes現在の作業ディレクトリとすべてのサブディレクトリに、結果を解析し、それぞれのターゲット・アーキテクチャを表示します。
使用されているのバージョンに応じて、corflags.exe出力の行項目には32BIT、または 32BITREQ(および32BITPREF)が含まれます。これら2つのどちらが出力に含まれるかは、Any CPUとを区別するために確認する必要がある重要な項目ですx86。古いバージョンのcorflags.exe(Windows SDK v8.0Aより前)を使用している場合は32BIT、他の人が過去の回答で示したように、出力にはラインアイテムのみが表示されます。それ以外の場合32BITREQと32BITPREFそれを置き換えます。
これcorflags.exeは、にあると想定してい%PATH%ます。これを確認する最も簡単な方法は、を使用することDeveloper Command Promptです。または、デフォルトの場所からコピーすることもできます。
以下のバッチファイルをアンマネージドdllまたはに対して実行すると、からの実際の出力は次のようなエラーメッセージになるため、exe誤ってとして表示されます。x86Corflags.exe
corflags:エラーCF008:指定されたファイルには有効な管理ヘッダーがありません
@echo off
echo.
echo Target architecture for all exes and dlls:
echo.
REM For each exe and dll in this directory and all subdirectories...
for %%a in (.exe, .dll) do forfiles /s /m *%%a /c "cmd /c echo @relpath" > testfiles.txt
for /f %%b in (testfiles.txt) do (
    REM Dump corflags results to a text file
    corflags /nologo %%b > corflagsdeets.txt
   REM Parse the corflags results to look for key markers   
   findstr /C:"PE32+">nul .\corflagsdeets.txt && (      
      REM `PE32+` indicates x64
        echo %%~b = x64
    ) || (
      REM pre-v8 Windows SDK listed only "32BIT" line item, 
      REM newer versions list "32BITREQ" and "32BITPREF" line items
        findstr /C:"32BITREQ  : 0">nul /C:"32BIT     : 0" .\corflagsdeets.txt && (
            REM `PE32` and NOT 32bit required indicates Any CPU
            echo %%~b = Any CPU
        ) || (
            REM `PE32` and 32bit required indicates x86
            echo %%~b = x86
        )
    )
    del corflagsdeets.txt
)
del testfiles.txt
echo.もう1つの方法は、DLLのVisual Studioツールからdumpbinを使用して、適切な出力を探すことです
dumpbin.exe /HEADERS <your dll path>
    FILE HEADER VALUE
                 14C machine (x86)
                   4 number of sections
            5885AC36 time date stamp Mon Jan 23 12:39:42 2017
                   0 file pointer to symbol table
                   0 number of symbols
                  E0 size of optional header
                2102 characteristics
                       Executable
                       32 bit word machine
                       DLL注:上記のo / pは32ビットdll用です
dumpbin.exeのもう1つの便利なオプションは/ EXPORTSで、DLLによって公開されている関数が表示されます
dumpbin.exe /EXPORTS <PATH OF THE DLL>より一般的な方法-ファイル構造を使用してビット数とイメージタイプを決定します。
public static CompilationMode GetCompilationMode(this FileInfo info)
{
    if (!info.Exists) throw new ArgumentException($"{info.FullName} does not exist");
    var intPtr = IntPtr.Zero;
    try
    {
        uint unmanagedBufferSize = 4096;
        intPtr = Marshal.AllocHGlobal((int)unmanagedBufferSize);
        using (var stream = File.Open(info.FullName, FileMode.Open, FileAccess.Read))
        {
            var bytes = new byte[unmanagedBufferSize];
            stream.Read(bytes, 0, bytes.Length);
            Marshal.Copy(bytes, 0, intPtr, bytes.Length);
        }
        //Check DOS header magic number
        if (Marshal.ReadInt16(intPtr) != 0x5a4d) return CompilationMode.Invalid;
        // This will get the address for the WinNT header  
        var ntHeaderAddressOffset = Marshal.ReadInt32(intPtr + 60);
        // Check WinNT header signature
        var signature = Marshal.ReadInt32(intPtr + ntHeaderAddressOffset);
        if (signature != 0x4550) return CompilationMode.Invalid;
        //Determine file bitness by reading magic from IMAGE_OPTIONAL_HEADER
        var magic = Marshal.ReadInt16(intPtr + ntHeaderAddressOffset + 24);
        var result = CompilationMode.Invalid;
        uint clrHeaderSize;
        if (magic == 0x10b)
        {
            clrHeaderSize = (uint)Marshal.ReadInt32(intPtr + ntHeaderAddressOffset + 24 + 208 + 4);
            result |= CompilationMode.Bit32;
        }
        else if (magic == 0x20b)
        {
            clrHeaderSize = (uint)Marshal.ReadInt32(intPtr + ntHeaderAddressOffset + 24 + 224 + 4);
            result |= CompilationMode.Bit64;
        }
        else return CompilationMode.Invalid;
        result |= clrHeaderSize != 0
            ? CompilationMode.CLR
            : CompilationMode.Native;
        return result;
    }
    finally
    {
        if (intPtr != IntPtr.Zero) Marshal.FreeHGlobal(intPtr);
    }
}コンパイルモードの列挙
[Flags]
public enum CompilationMode
{
    Invalid = 0,
    Native = 0x1,
    CLR = Native << 1,
    Bit32 = CLR << 1,
    Bit64 = Bit32 << 1
}GitHubでの説明付きのソースコード
利用可能なすべての情報を表示するために、Windowsエクスプローラーにアセンブリのコンテキストメニューエントリを追加する非常に便利なツールを複製しました。
ここからダウンロード:https : //github.com/tebjan/AssemblyInformation/releases
.NETアセンブリのターゲットプラットフォームを確認する別の方法は、.NETリフレクターを使用してアセンブリを検査することです...
@#〜#€〜!新しいバージョンが無料ではないことに気づきました!したがって、.NETリフレクターの無料バージョンをお持ちの場合は、それを使用してターゲットプラットフォームを確認できます。
cfedukeは、GetPEKindを呼び出す可能性を指摘しています。PowerShellからこれを実行することは、潜在的に興味深いことです。
たとえば、次のコードは、使用可能なコマンドレットのコードです。https://stackoverflow.com/a/16181743/64257
または、https: //stackoverflow.com/a/4719567/64257で、「実行可能なイメージのテストに使用できるPowerShellコミュニティ拡張機能のGet-PEHeaderコマンドレットもあります」と記載されています。
そのためのより高度なアプリケーションは、CodePlex-ApiChangeにあります。
例:
C:\Downloads\ApiChange>ApiChange.exe -CorFlags c:\Windows\winhlp32.exe
File Name; Type; Size; Processor; IL Only; Signed
winhlp32.exe; Unmanaged; 296960; X86
C:\Downloads\ApiChange>ApiChange.exe -CorFlags c:\Windows\HelpPane.exe
File Name; Type; Size; Processor; IL Only; Signed
HelpPane.exe; Unmanaged; 733696; Amd64すでに述べたツールの代わりに、Telerik JustDecompile(無料ツール)があり、アセンブリ名の横に情報が表示されます。