Get Platform RRS feed

  • Question

  • Hey,

    Gonna detect if the platform is x86 or x64, have 2 code snippets, just confused which one to use, although both seem to work on Win10 x64:


    If Environment.Is64BitOperatingSystem Then... Else...


    If Reflection.AssemblyName.GetAssemblyName(Application.ExecutablePath).ProcessorArchitecture = Reflection.ProcessorArchitecture.X86 Then... Else...

    I don't remember why I used 2 in 2 similar apps, but I recall one had problem on some systems, can't remember which one always works, any advise please? :)

    Saturday, November 28, 2020 10:03 PM

All replies

  • Another option

    if (IntPtr.Size == 8)
    else if (IntPtr.Size == 4)

    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.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    Sunday, November 29, 2020 12:15 AM
  • Another option

    if (IntPtr.Size == 8)
    else if (IntPtr.Size == 4)

    IntPtr.Size = 4  for a 32-bit process on x64 OS
    Sunday, November 29, 2020 12:53 AM
  • Do you want to detect the kind of Operation System or the kind of your running application?

    Sunday, November 29, 2020 3:19 AM
  • Nah just platform, I know I should set my app to Any CPU to get correct result on x64 PCs.

    BTW, I just was confused if my 2 methods and now 3rd one by dear Karen, which one's working on all OS versions, so gonna install all one by one both in x64/x86 on VirtualBox to test :(

    Sunday, November 29, 2020 12:07 PM
  • Is64BitOperatingSystem should work on all versions

    (you can see the source at environment.cs)

    Another method used in C++ is GetNativeSystemInfo

    Dim bIs64BitOS As Boolean = False
    Dim si As SYSTEM_INFO
    If ((PROCESSOR_ARCHITECTURE_IA64 = si.wProcessorArchitecture) Or (PROCESSOR_ARCHITECTURE_AMD64 = si.wProcessorArchitecture) Or (PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 = si.wProcessorArchitecture) Or
        (PROCESSOR_ARCHITECTURE_ALPHA64 = si.wProcessorArchitecture) Or (PROCESSOR_ARCHITECTURE_ARM32_ON_WIN64 = si.wProcessorArchitecture) Or (PROCESSOR_ARCHITECTURE_IA32_ON_ARM64 = si.wProcessorArchitecture)) Then
        bIs64BitOS = True
    End If
    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Shared Sub GetNativeSystemInfo(ByRef lpSystemInfo As SYSTEM_INFO)
    End Sub
    Public Structure SYSTEM_INFO
        Public wProcessorArchitecture As UShort
        Public wReserved As UShort
        Public dwPageSize As UInteger
        Public lpMinimumApplicationAddress As IntPtr
        Public lpMaximumApplicationAddress As IntPtr
        Public dwActiveProcessorMask As IntPtr
        Public dwNumberOfProcessors As Integer
        Public dwProcessorType As Integer
        Public dwAllocationGranularity As Integer
        Public wProcessorLevel As Short
        Public wProcessorRevision As Short
    End Structure
    Public Const PROCESSOR_ARCHITECTURE_INTEL = 0          
    Public Const PROCESSOR_ARCHITECTURE_MIPS = 1           
    Public Const PROCESSOR_ARCHITECTURE_ALPHA = 2          
    Public Const PROCESSOR_ARCHITECTURE_PPC = 3            
    Public Const PROCESSOR_ARCHITECTURE_SHX = 4            
    Public Const PROCESSOR_ARCHITECTURE_ARM = 5            
    Public Const PROCESSOR_ARCHITECTURE_IA64 = 6           
    Public Const PROCESSOR_ARCHITECTURE_ALPHA64 = 7        
    Public Const PROCESSOR_ARCHITECTURE_MSIL = 8           
    Public Const PROCESSOR_ARCHITECTURE_AMD64 = 9          
    Public Const PROCESSOR_ARCHITECTURE_ARM64 = 12         

    Sunday, November 29, 2020 1:05 PM
  • Just keep in mind that if you compile using ANY cpu?

    then the bit size you get will depend on if you launch the .exe from the x32 cmd prompt or the x64 one.

    and while developing in VS? Then your app will always run as x32 since VS is a x32 bit application. You can force the project to x64 in VS - and then when you launch/debug, it will in fact run as x64. (but ANY will always run as x32 from VS).

    In fact, we get/see a lot of questions about using ACE (MS-Access data engine). Users attempting to use the x64 bit version CAN use the VS connection builders, but using test connection WILL ALWAYS fail in VS, since VS is a x32 bit program. You can use the connection builders in VS for this - but the test connection for a un-managed library (such as ms-access) will NOT work - you have to run the code for the connection to work. So keep in mind that some libraries and features in VS will fail in the IDE, but work when you run the code as x64.

    To get/test the current bit size the app is running? I use this:

          Dim strMsg As String
            ' set lable  to display x32 or x64 bit process
            If Environment.Is64BitProcess = True Then
                strMsg = "This is running as 64 bit process"
                strMsg = "This is running as 32 bit process"
            End If

            ' set lable to display if OS is x32 or x64 bits
            If Environment.Is64BitOperatingSystem = True Then
                strMsg = strMsg & " (Windows OS = 64 bits)"
                strMsg = strMsg & " (Windows OS = 32 bits)"
            End If
            Me.lblProcess.Text = strMsg

    So above shows how to get OS and how to get .net assembly process running as. In most cases, of course you only care about the .net running as, as you don't really have control over the OS bit size - but x32 is quite rare. Obviously if you on a x32 bit machine, then you .net code will be running as x32.

    So, in near all cases - I only really need + care about the .net code - not what bit size the OS is.

    The result of strMsg shoved into a label thus looks like this:


    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    Thursday, December 3, 2020 6:31 PM