64 Bit Detection

22. May 2012 17:53


I’ve used this code on a few projects which needed to know if they were running on a 64 bit host.


  Private Shared Function Is64BitOs() As Boolean
        If IntPtr.Size = 8 Or (IntPtr.Size = 4 And Is32BitProcessOn64BitProcessor()) Then
            Return True
            Return False
        End If
    End Function

    Private Shared Function Is32BitProcessOn64BitProcessor() As Boolean
        Dim retVal As Boolean = False
            ' clear any exising error
            SetLastErrorEx(0, 0)
            If IsWow64Process(Process.GetCurrentProcess.Handle, retVal) Then
                Return retVal
                ' function call failed
                Throw New Exception(String.Format("IsWow64Process failed, lastError = {0}", Marshal.GetLastWin32Error))
            End If
        Catch ex As Exception
        End Try
    End Function

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

    End Function

    <DllImport("user32.dll")> _
    Private Shared Sub SetLastErrorEx(ByVal dwErrCode As UInteger, ByVal dwType As UInteger)
    End Sub

About the author

I'm a .NET developer, a husband and a father of three beautiful girls.

Month List