JLION.COM
Class for detecting attributes of host operating system
1/10/2010 VB.NET

I put together this SystemInfo class while I was working on vShadow, a wrapper for the windows volume shadow copy functions. My Volume Shadow Copy wrapper is a C DLL and has to be compiled for specific operating systems. IE: The DLL compiled for 32-bit Windows XP won't work on 64-bit Windows Vista. It turns out that there are a number of different flavors of Microsoft Operating system and the built-in managed code way of detecting versions (System.Environment.OSVersion doesn't tell you whether the host O/S is 32-bit or 64-bit). There is an API that does, however.

The SystemInfo class is really just a wrapper for the GetNativeSystemInfo API. This API gives you TONS of information about the host O/S. SystemInfo in its current incarnation doesn't return all the information available from GetNativeSystemInfo-- just enough to let you know what version of O/S you're running under, whether it is 32 or 64 bit and what kind of processor is being used (IE: is it IA64?)

It should be possible to use SystemInfo and the techniques used in vShadow to provide a single executable for most any application.

If there's a better way to do this (than having a separate DLL for each version and flavor of the O/S) than I'd love to hear about it. In the meantime, this approach has worked for me so far.

 Imports System.Runtime.InteropServices

Friend Class SystemInfo
    'By Joe Lynds, 2008
    'http://www.jlion.com

     _
    Private Structure SYSTEM_INFO
        Dim wProcessorArchitecture As Int16
        Dim wReserved As Int16
        Dim dwPageSize As Integer
        Dim lpMinimumApplicationAddress As Integer
        Dim lpMaximumApplicationAddress As Integer
        Dim dwActiveProcessorMask As Integer
        Dim dwNumberOfProcessors As Integer
        Dim dwProcessorType As Integer
        Dim dwAllocationGranularity As Integer
        Dim wProcessorLevel As Int16
        Dim wProcessorRevision As Int16
    End Structure

    Private Declare Sub GetNativeSystemInfo Lib "kernel32" ( ByRef lpSystemInfo As SYSTEM_INFO)

    Public Enum PROCESSOR_ARCHITECTURE As UShort
        PROCESSOR_ARCHITECTURE_INTEL = 0
        PROCESSOR_ARCHITECTURE_IA64as = 6
        PROCESSOR_ARCHITECTURE_AMD64 = 9
        PROCESSOR_ARCHITECTURE_UNKNOWN = &HFFFF
    End Enum

    Public Enum PROCESSOR As Long
        PROCESSOR_INTEL_386 = 386
        PROCESSOR_INTEL_486 = 486
        PROCESSOR_INTEL_PENTIUM = 586
        PROCESSOR_INTEL_IA64 = 2200
        PROCESSOR_AMD_X8664 = 8664
    End Enum

    Private miProcessorArchitecture As PROCESSOR_ARCHITECTURE
    Private miProcessorType As PROCESSOR

    Public Sub New()
        Dim oInfo As SYSTEM_INFO
        GetNativeSystemInfo(oInfo)

        With oInfo
            miProcessorArchitecture = .wProcessorArchitecture
            miProcessorType = .dwProcessorType
        End With
    End Sub

    Public ReadOnly Property ProcessorArchitecture() As PROCESSOR_ARCHITECTURE
        Get
            Return miProcessorArchitecture
        End Get
    End Property

    Public ReadOnly Property ProcessorType() As PROCESSOR
        Get
            Return miProcessorType
        End Get
    End Property

    Public ReadOnly Property Is64Bit() As Boolean
        Get
            If miProcessorArchitecture = SystemInfo.PROCESSOR_ARCHITECTURE.PROCESSOR_ARCHITECTURE_AMD64 _
                OrElse miProcessorArchitecture = SystemInfo.PROCESSOR_ARCHITECTURE.PROCESSOR_ARCHITECTURE_IA64as Then
                '---64-bit
                Return True
            Else
                '--32-bit
                Return False
            End If
        End Get
    End Property
End Class