Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if OS is 32 Bit OS or 64 Bit

Tags:

vb.net

Is it possible to check if computer is 32 bit or 64 using vb.net code? I just want to display the result in a message.

Please advise.

like image 685
Furqan Sehgal Avatar asked Jan 20 '13 09:01

Furqan Sehgal


3 Answers

Environment.Is64BitOperatingSystem should do nicely.

Determines whether the current operating system is a 64-bit operating system.

The assumption being that a false signifies a 32bit environment.

If you want to find out if the process is 64bit (as you can run a 32bit process on a 64bit OS), use Environment.Is64BitProcess:

Determines whether the current process is a 64-bit process.


Both of these have been introduced in .NET 4.0.

like image 116
Oded Avatar answered Oct 03 '22 01:10

Oded


IntPtr.Size won't return the correct value if running in 32-bit .NET Framework 2.0 on 64-bit Windows (it would return 32-bit).

You have to first check if running in a 64-bit process (I think in .NET you can do so by checking IntPtr.Size), and if you are running in a 32-bit process, you still have to call the Win API function IsWow64Process. If this returns true, you are running in a 32-bit process on 64-bit Windows.

Microsoft's Raymond Chen: How to detect programmatically whether you are running on 64-bit Windows

Solution:

Private is64BitProcess As Boolean = (IntPtr.Size = 8)
Private is64BitOperatingSystem As Boolean = is64BitProcess OrElse InternalCheckIsWow64()

<DllImport("Kernel32.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi)> _
    Public Shared Function IsWow64Process( _
    ByVal hProcess As Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid, _
    ByRef wow64Process As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean

    End Function

Public Shared Function InternalCheckIsWow64() As Boolean
    If (Environment.OSVersion.Version.Major = 5 AndAlso Environment.OSVersion.Version.Minor >= 1) OrElse Environment.OSVersion.Version.Major >= 6 Then
        Using p As Process = Process.GetCurrentProcess()
            Dim retVal As Boolean
            If Not IsWow64Process(p.Handle, retVal) Then
                Return False
            End If
            Return retVal
        End Using
    Else
        Return False
    End If
End Function
like image 38
Parimal Raj Avatar answered Oct 03 '22 02:10

Parimal Raj


I simply use this piece of code and it works fine:

If System.Environment.Is64BitOperatingSystem = True Then
    MessageBox.Show("OS System : 64 Bit Operating System")
Else
    MessageBox.Show("OS System : 32 Bit Operating System")
End If
like image 39
Rangga Stephen Avatar answered Oct 03 '22 02:10

Rangga Stephen