none
Get my app architecture RRS feed

  • Question

  • Hi,

    My VB.NET app (.NET FW4.0) is compiled with either AnyCPU or x86 tag. At runtime, can I get the current app architecture? For example, if it's x86 or x64?

    I mean application architecture, not OS architecture (Environment.Is64BitOperatingSystem)!

    Anyway to get it in .NET?

    Sunday, March 3, 2019 5:39 PM

Answers

  • Thanks, can you please check your code, 1st & 2nd items of Select Case, seems the same! Both Amd64!

    This is something you could had done which is what Intellisense is all about.

    Imports System.Reflection
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim test = Reflection.AssemblyName.GetAssemblyName(Application.ExecutablePath)
            Select Case test.ProcessorArchitecture
                Case ProcessorArchitecture.Amd64
                    ' A 64-bit processor based on the x64 architecture.
                Case ProcessorArchitecture.Arm
                    ' An ARM processor.
                Case ProcessorArchitecture.IA64
                    ' A 64-bit Intel Itanium processor only.
                Case ProcessorArchitecture.MSIL
                    ' Neutral with respect to processor and bits-per-word.
                Case ProcessorArchitecture.X86
                    '  A 32-bit Intel processor, either native or in the Windows on Windows environment on a 64-bit platform (WOW64).
                Case ProcessorArchitecture.None
                    ' An unknown or unspecified combination of processor and bits-per-word
            End Select
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by OSVBNET Sunday, March 3, 2019 7:43 PM
    Sunday, March 3, 2019 6:38 PM
    Moderator

All replies

  • Hello,

    Give this a run

    Imports System.Reflection
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim test = Reflection.AssemblyName.GetAssemblyName(Application.ExecutablePath)
            Select Case test.ProcessorArchitecture
                Case ProcessorArchitecture.Amd64
                    ' A 64-bit processor based on the x64 architecture.
                Case ProcessorArchitecture.Amd64
                    ' An ARM processor.
                Case ProcessorArchitecture.IA64
                    ' A 64-bit Intel Itanium processor only.
                Case ProcessorArchitecture.MSIL
                    ' Neutral with respect to processor and bits-per-word.
                Case ProcessorArchitecture.X86
                    '  A 32-bit Intel processor, either native or in the Windows on Windows environment on a 64-bit platform (WOW64).
                Case ProcessorArchitecture.None
                    ' An unknown or unspecified combination of processor and bits-per-word
            End Select
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 3, 2019 5:52 PM
    Moderator
  • Thanks, can you please check your code, 1st & 2nd items of Select Case, seems the same! Both Amd64!
    Sunday, March 3, 2019 5:58 PM
  • Hi,
    Remarks
    Beginning with the .NET Framework 4, this property always returns ProcessorArchitecture.None for reference assemblies.


    --
    Viele Grüsse
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Sunday, March 3, 2019 6:03 PM
  • Also, my PC is Intel Core i7 7700K - Windows 10 x64 Ent 1803.

    When I compile my app with x86 profile, I get "x86" which is right; BUT:

    When I compile my app with AnyCPU profile, I get "MSIL", which is not expected I think?

    Sunday, March 3, 2019 6:04 PM
  • Peter, those Remarks say that always .None?

    For me, not .None at all, either x86 or MSIL, BTW, what is reference assemblies?

    Sunday, March 3, 2019 6:15 PM
  • Thanks, can you please check your code, 1st & 2nd items of Select Case, seems the same! Both Amd64!

    This is something you could had done which is what Intellisense is all about.

    Imports System.Reflection
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim test = Reflection.AssemblyName.GetAssemblyName(Application.ExecutablePath)
            Select Case test.ProcessorArchitecture
                Case ProcessorArchitecture.Amd64
                    ' A 64-bit processor based on the x64 architecture.
                Case ProcessorArchitecture.Arm
                    ' An ARM processor.
                Case ProcessorArchitecture.IA64
                    ' A 64-bit Intel Itanium processor only.
                Case ProcessorArchitecture.MSIL
                    ' Neutral with respect to processor and bits-per-word.
                Case ProcessorArchitecture.X86
                    '  A 32-bit Intel processor, either native or in the Windows on Windows environment on a 64-bit platform (WOW64).
                Case ProcessorArchitecture.None
                    ' An unknown or unspecified combination of processor and bits-per-word
            End Select
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by OSVBNET Sunday, March 3, 2019 7:43 PM
    Sunday, March 3, 2019 6:38 PM
    Moderator
  • Also, my PC is Intel Core i7 7700K - Windows 10 x64 Ent 1803.

    When I compile my app with x86 profile, I get "x86" which is right; BUT:

    When I compile my app with AnyCPU profile, I get "MSIL", which is not expected I think?

    Have you read the documentation and my comment in the case statement?

    https://docs.microsoft.com/en-us/dotnet/api/system.applicationid.processorarchitecture?view=netframework-4.7.2


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Sunday, March 3, 2019 6:40 PM
    Moderator
  • You can use GetBinaryType

    (tested on Windows 10)

        <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function GetBinaryType(lpApplicationName As String, ByRef lpBinaryType As Integer) As Boolean
        End Function
    
        Public Const SCS_32BIT_BINARY = 0
        Public Const SCS_DOS_BINARY = 1
        Public Const SCS_WOW_BINARY = 2
        Public Const SCS_PIF_BINARY = 3
        Public Const SCS_POSIX_BINARY = 4
        Public Const SCS_OS216_BINARY = 5
        Public Const SCS_64BIT_BINARY = 6

    Sunday, March 3, 2019 6:43 PM
  • Hi,
    to demonstrate check this:

      Sub Main()
        Dim test = System.Reflection.Assembly.GetExecutingAssembly().GetName(True)
        Console.WriteLine($" - {test.ProcessorArchitecture}")
    
        Console.WriteLine()
    
        For Each item In System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies
          Console.WriteLine($"{item.Name} - {item.ProcessorArchitecture}")
        Next
    
        Console.ReadKey()
      End Sub

    Result may be:

     - MSIL
    mscorlib - None
    System - None
    Microsoft.VisualBasic - None


    --
    Viele Grüsse
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Sunday, March 3, 2019 7:09 PM