[.net] .NET 어셈블리가 x86 또는 x64 용으로 구축되었는지 어떻게 알 수 있습니까?

.NET 어셈블리의 임의 목록이 있습니다.

프로그래밍 방식으로 각 DLL이 x86 또는 모든 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 :
코드베이스 : file : /// C : / projects / powershell / BuildAnalyzer / ...
EscapedCodeBase : 파일 : /// C : / projects / powershell / BuildAnalyzer / ...
프로세서 아키텍처 : MSIL
플래그 : PublicKey
해시 알고리즘 : SHA1
버전 호환성 : SameMachine
키 페어 :
성명 : Microsoft.GLEE, Version = 1.0.0.0, Culture = neut ...

여기서 ProcessorArchitecture 는 대상 플랫폼을 식별합니다.

  • Amd64 : x64 아키텍처 기반의 64 비트 프로세서입니다.
  • Arm : ARM 프로세서
  • IA64 : 64 비트 Intel Itanium 프로세서 만 해당합니다.
  • MSIL : 프로세서 및 워드 당 비트 수와 관련하여 중립입니다.
  • X86 : 기본 또는 64 비트 플랫폼 (WOW64)의 Windows 기반 Windows 환경에있는 32 비트 Intel 프로세서.
  • None : 프로세서와 워드 당 비트의 알 수 없거나 지정되지 않은 조합입니다.

이 예제에서는 PowerShell을 사용하여 메서드를 호출하고 있습니다.


답변

당신은 사용할 수 있습니다 CorFlags CLI의 도구를 (예를 들어, C는 : \ 프로그램 파일 \은 Microsoft SDKs \ 윈도우 \ 7.0 \ 빈 \ CorFlags.exe가)의 상태를 결정하는 어셈블리, 그것의 출력과 같은 조립를 여는 기반 이진 자산 32BIT 플래그가 1 ( x86 ) 또는 0 ( 에 따라 임의의 CPU 또는 x64 )으로 설정되어 있는지 확인해야 할 위치를 결정할 수 있어야합니다 PE.

Option    | PE    | 32BIT
----------|-------|---------
x86       | PE32  | 1
Any CPU   | PE32  | 0
x64       | PE32+ | 0

.NET사용한 x64 Development 블로그 게시물 에 대한 정보가 corflags있습니다.

더 좋은 방법 은 어셈블리가 다른 속성과 함께 값 (64 비트), (32 비트 및 WOW) 또는 (모든 CPU) 인지 확인하는 데 사용할Module.GetPEKind있습니다 .PortableExecutableKindsPE32PlusRequired32BitILOnly


답변

명확히하기 위해 CorFlags.exe는 .NET Framework SDK의 일부입니다 . 내 컴퓨터에 개발 도구가 있으며 DLL이 32 비트인지 여부를 결정하는 가장 간단한 방법은 다음과 같습니다.

  1. Visual Studio 명령 프롬프트를 엽니 다 (Windows : 메뉴 시작 / 프로그램 / Microsoft Visual Studio / Visual Studio 도구 / Visual Studio 2008 명령 프롬프트)

  2. 해당 DLL이 포함 된 디렉토리의 CD

  3. 다음과 같이 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

의견에 따라 위의 플래그는 다음과 같이 읽습니다.

  • 모든 CPU : PE = PE32 및 32BIT = 0
  • x86 : PE = PE32 및 32BIT = 1
  • 64 비트 : PE = PE32 + 및 32BIT = 0

답변

당신은 어떻게 당신이 자신을 작성? 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 사용해보십시오 . 다른 어셈블리에 대한 참조가 없으며 그대로 사용할 수 있습니다.


답변

JetBrians의 DotPeek는 msil (anycpu), x86, x64를 빠르고 쉽게 볼 수있는 방법을 제공합니다
닷픽


답변

[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);
    }
}