Get Assembly Platform
Determines the target platform of a given .NET assembly (e.g., x64, x86, AnyCPU).
This function uses GetPEKind to analyze the assembly’s PE header and determine platform characteristics:
- Returns “x64” if compiled for AMD64 or IA64, or if PE32Plus is set.
- Returns “x86” if Required32Bit is set.
- Returns “AnyCPU” if the assembly is IL-only and not restricted to 32-bit.
VB
Public Function GetAssemblyPlatform(assembly As Reflection.Assembly) As String
Try
Dim peKind As Reflection.PortableExecutableKinds
Dim machine As Reflection.ImageFileMachine
assembly.ManifestModule.GetPEKind(peKind, machine)
' If the header is PE32+ or the machine is AMD64 or IA64, it's 64-bit.
If (peKind And Reflection.PortableExecutableKinds.PE32Plus) <> 0 OrElse
machine = Reflection.ImageFileMachine.AMD64 OrElse
machine = Reflection.ImageFileMachine.IA64 Then
Return "x64"
End If
' If flag Required32Bit is set, the assembly is forced to 32-bit.
If (peKind And Reflection.PortableExecutableKinds.Required32Bit) <> 0 Then
Return "x86"
End If
' If the assembly is IL-only, it's AnyCPU.
If (peKind And Reflection.PortableExecutableKinds.ILOnly) <> 0 Then
Return "AnyCPU"
End If
Return "Unknown"
Catch ex As Exception
Console.WriteLine("Errore during assembly analysis: " & ex.Message)
Return "Unknown"
End Try
End Function