| 

.NET C# Java Javascript Exception

8
Wie kann ich prüfen, ob meine Anwendung unter einem 32- oder 64-Bit-Windows-System ausgeführt wird?
05.02.2011
westavella 81 1 3
5 Antworten
8
Seit .NET 4 gibt es eine ganz simple, eingebaute Lösung:

Environment.Is64BitProcess

und

Environment.Is64BitOperatingSystem

Die prüfen intern unter anderem auch, ob eine 32 Bit-App auf einem 64 Bit-Windows läuft (WoW), sind also wohl nicht nur einfach, sondern auch zuverlässig.
05.02.2011
Golo Roden 2,7k 3 9
+1 perfekt : )
Mario Priebe 05.02.2011
2
Update: Achtung für .NET Versionen < 4.0, ab 4.0 siehe Golo Roden seine Antwort.

Um auf der sicheren Seite zu sein, schau dir mal die Hilfsmethoden von Andrew Ensley an.

using System;
using System.Management;
using System.Text.RegularExpressions;

/// <summary>
/// Gets Operating System Name, Service Pack, and Architecture using WMI with the legacy methods as a fallback
/// </summary>
/// <returns>String containing the name of the operating system followed by its service pack (if any) and architecture</returns>
private string getOSInfo()
{
ManagementObjectSearcher objMOS = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");

//Variables to hold our return value
string os = "";
int OSArch = 0;

try
{
foreach (ManagementObject objManagement in objMOS.Get())
{
// Get OS version from WMI - This also gives us the edition
object osCaption = objManagement.GetPropertyValue("Caption");
if (osCaption != null)
{
// Remove all non-alphanumeric characters so that only letters, numbers, and spaces are left.
string osC = Regex.Replace(osCaption.ToString(),"[^A-Za-z0-9 ]","");
//string osC = osCaption.ToString();
// If the OS starts with "Microsoft," remove it. We know that already
if (osC.StartsWith("Microsoft"))
{
osC = osC.Substring(9);
}
// If the OS now starts with "Windows," again... useless. Remove it.
if (osC.Trim().StartsWith("Windows"))
{
osC = osC.Trim().Substring(7);
}
// Remove any remaining beginning or ending spaces.
os = osC.Trim();
// Only proceed if we actually have an OS version - service pack is useless without the OS version.
if(!String.IsNullOrEmpty(os))
{
object osSP = null;
try
{
// Get OS service pack from WMI
osSP = objManagement.GetPropertyValue("ServicePackMajorVersion");
if (osSP != null && osSP.ToString() != "0")
{
os += " Service Pack " + osSP.ToString();
}
else
{
// Service Pack not found. Try built-in Environment class.
os += getOSServicePackLegacy();
}
}
catch (Exception)
{
// There was a problem getting the service pack from WMI. Try built-in Environment class.
os += getOSServicePackLegacy();
}
}
object osA = null;
try
{
// Get OS architecture from WMI
osA = objManagement.GetPropertyValue("OSArchitecture");
if (osA != null)
{
string osAString = osA.ToString();
// If "64" is anywhere in there, it's a 64-bit architectore.
OSArch = (osAString.Contains("64") ? 64 : 32);
}
}
catch (Exception)
{
}
}
}
}
catch (Exception)
{
}
// If WMI couldn't tell us the OS, use our legacy method.
// We won't get the exact OS edition, but something is better than nothing.
if (os == "")
{
os = getOSLegacy();
}
// If WMI couldn't tell us the architecture, use our legacy method.
if (OSArch == 0)
{
OSArch = getOSArchitectureLegacy();
}
return os + " " + OSArch.ToString() + "-bit";
}

/// <summary>
/// Gets Operating System Name using .Net's Environment class.
/// </summary>
/// <returns>String containing the name of the operating system followed by its service pack (if any)</returns>
private string getOSLegacy()
{
//Get Operating system information.
OperatingSystem os = Environment.OSVersion;
//Get version information about the os.
Version vs = os.Version;

//Variable to hold our return value
string operatingSystem = "";

if (os.Platform == PlatformID.Win32Windows)
{
//This is a pre-NT version of Windows
switch (vs.Minor)
{
case 0:
operatingSystem = "95";
break;
case 10:
if (vs.Revision.ToString() == "2222A")
operatingSystem = "98SE";
else
operatingSystem = "98";
break;
case 90:
operatingSystem = "Me";
break;
default:
break;
}
}
else if(os.Platform == PlatformID.Win32NT)
{
switch (vs.Major)
{
case 3:
operatingSystem = "NT 3.51";
break;
case 4:
operatingSystem = "NT 4.0";
break;
case 5:
if (vs.Minor == 0)
{
operatingSystem = "2000";
}
else
{
operatingSystem = "XP";
}
break;
case 6:
if (vs.Minor == 0)
{
operatingSystem = "Vista";
}
else
{
operatingSystem = "7";
}
break;
default:
break;
}
}
//Make sure we actually got something in our OS check
//We don't want to just return " Service Pack 2"
//That information is useless without the OS version.
if(operatingSystem != "")
{
//Got something. Let's see if there's a service pack installed.
operatingSystem += getOSServicePackLegacy();
}
//Return the information we've gathered.
return operatingSystem;
}

/// <summary>
/// Gets the installed Operating System Service Pack using .Net's Environment class.
/// </summary>
/// <returns>String containing the operating system's installed service pack (if any)</returns>
private string getOSServicePackLegacy()
{
// Get service pack from Environment Class
string sp = Environment.OSVersion.ServicePack;
if (sp != null && sp.ToString() != "" && sp.ToString() != " ")
{
// If there's a service pack, return it with a space in front (for formatting)
return " " + sp.ToString();
}
// No service pack. Return an empty string
return "";
}

/// <summary>
/// Gets Operating System Architecture. This does not tell you if the program in running in
/// 32- or 64-bit mode or if the CPU is 64-bit capable. It tells you whether the actual Operating
/// System is 32- or 64-bit.
/// </summary>
/// <returns>Int containing 32 or 64 representing the number of bits in the OS Architecture</returns>
private int getOSArchitectureLegacy()
{
string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
return ((String.IsNullOrEmpty(pa) || String.Compare(pa,0,"x86",0,3,true) == 0) ? 32 : 64);
}
05.02.2011
Mario Priebe 6,0k 3 9
Ich muss diesen ganzen Codebatzen ausführen, nur um zu testen ob es sich um ein 32- oder 64 Bit System handelt?
buk 09.02.2011
1
zur Hilfsmethode getOSArchitectureLegacy()
Prozess:
pa1 = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");

OS:
pa2 = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine);

Den Unterschied sieht man dann, wenn die 32Bit-App auf einer 64Bit Maschine läuft (WoW):
pa1 == 32 und pa2 == 64,
IntPtr.Size * 8
liefert dann auch 32.
08.02.2011
lfgb 11 1
0
Kein Problem, du musst einfach die länge der Pointers prüfen:

var size = sizeof(IntPtr);


Bei size == 8 ist es x64, bei size == 4 ist es x86.
05.02.2011
BenWagner 204 3
1
oder int size = IntPtr.Size;
Mario Priebe 05.02.2011
Ich glaube das ist nicht wirklich das gesuchte, jedenfalls in meinem Test zeigte der in einer Konsolenanwendung size= 4 an, obwohl ich ein 64Bit System habe.
Mario Priebe 05.02.2011
1
Das genügt nicht, weil eine als 32 Bit kompilierte Anwendung immer für int 4 Byte zurückgibt, egal, ob das zu Grunde liegende Windows 32 oder 64 Bit hat (WoW).
Golo Roden 05.02.2011
Stimmt, das geht nur mit Any-CPU kompilat.
BenWagner 05.02.2011
0
Imports System.Runtime.InteropServices
Imports System.Text

''' <summary>
''' Klasse stellt erweiterte Informationen zur Prozessor- und Betriebssystemarchitektur zur Verfügung.
''' </summary>
''' <remarks>Leider gibt die Klasse System.Environment.OSVersion nicht genügend Aufschluss darüber, welches Programm
''' <para>auf welchem Betriebssystem mit welchem Prozessor (jeweils 32bit oder 64bit) ausgeführt wird.</para>
''' <para>Dieses Manko wird mit dieser Klasse behoben.</para>
''' </remarks>
Public NotInheritable Class OSVersion

#Region "initialization"

Shared m_Instance As OSVersion
Shared Sub New()
m_Instance = New OSVersion
End Sub

Private Sub New()
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub

Public Shared ReadOnly Property Instance() As OSVersion
Get
Return m_Instance
End Get
End Property

#End Region

#Region "Enums"

Public Enum SoftwareArchitecture
Unknown = 0
Bit32 = 1
Bit64 = 2
End Enum

Public Enum ProcessorArchitecture
Unknown = 0
Bit32 = 1
Bit64 = 2
Itanium64 = 3
End Enum

#End Region

#Region "Structures"

#Region "OSVERSIONINFOEX"

<StructLayout(LayoutKind.Sequential)> _
Private Structure OSVERSIONINFOEX
Public dwOSVersionInfoSize As Integer
Public dwMajorVersion As Integer
Public dwMinorVersion As Integer
Public dwBuildNumber As Integer
Public dwPlatformId As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
Public szCSDVersion As String
Public wServicePackMajor As Short
Public wServicePackMinor As Short
Public wSuiteMask As Short
Public wProductType As Byte
Public wReserved As Byte
End Structure

#End Region

#Region "_PROCESSOR_INFO_UNION"

<StructLayout(LayoutKind.Explicit)> _
Private Structure _PROCESSOR_INFO_UNION
<FieldOffset(0)> _
Friend dwOemId As UInteger
<FieldOffset(0)> _
Friend wProcessorArchitecture As UShort
<FieldOffset(2)> _
Friend wReserved As UShort
End Structure

#End Region

#Region "SYSTEM_INFO"

<StructLayout(LayoutKind.Sequential)> _
Private Structure SYSTEM_INFO
Friend uProcessorInfo As _PROCESSOR_INFO_UNION
Public dwPageSize As UInteger
Public lpMinimumApplicationAddress As IntPtr
Public lpMaximumApplicationAddress As IntPtr
Public dwActiveProcessorMask As IntPtr
Public dwNumberOfProcessors As UInteger
Public dwProcessorType As UInteger
Public dwAllocationGranularity As UInteger
Public dwProcessorLevel As UShort
Public dwProcessorRevision As UShort
End Structure

#End Region

#End Region

#Region "Variables"

#Region "PRODUCT"
Private Const PRODUCT_UNDEFINED As Integer = &H0
Private Const PRODUCT_ULTIMATE As Integer = &H1
Private Const PRODUCT_HOME_BASIC As Integer = &H2
Private Const PRODUCT_HOME_PREMIUM As Integer = &H3
Private Const PRODUCT_ENTERPRISE As Integer = &H4
Private Const PRODUCT_HOME_BASIC_N As Integer = &H5
Private Const PRODUCT_BUSINESS As Integer = &H6
Private Const PRODUCT_STANDARD_SERVER As Integer = &H7
Private Const PRODUCT_DATACENTER_SERVER As Integer = &H8
Private Const PRODUCT_SMALLBUSINESS_SERVER As Integer = &H9
Private Const PRODUCT_ENTERPRISE_SERVER As Integer = &HA
Private Const PRODUCT_STARTER As Integer = &HB
Private Const PRODUCT_DATACENTER_SERVER_CORE As Integer = &HC
Private Const PRODUCT_STANDARD_SERVER_CORE As Integer = &HD
Private Const PRODUCT_ENTERPRISE_SERVER_CORE As Integer = &HE
Private Const PRODUCT_ENTERPRISE_SERVER_IA64 As Integer = &HF
Private Const PRODUCT_BUSINESS_N As Integer = &H10
Private Const PRODUCT_WEB_SERVER As Integer = &H11
Private Const PRODUCT_CLUSTER_SERVER As Integer = &H12
Private Const PRODUCT_HOME_SERVER As Integer = &H13
Private Const PRODUCT_STORAGE_EXPRESS_SERVER As Integer = &H14
Private Const PRODUCT_STORAGE_STANDARD_SERVER As Integer = &H15
Private Const PRODUCT_STORAGE_WORKGROUP_SERVER As Integer = &H16
Private Const PRODUCT_STORAGE_ENTERPRISE_SERVER As Integer = &H17
Private Const PRODUCT_SERVER_FOR_SMALLBUSINESS As Integer = &H18
Private Const PRODUCT_SMALLBUSINESS_SERVER_PREMIUM As Integer = &H19
Private Const PRODUCT_HOME_PREMIUM_N As Integer = &H1A
Private Const PRODUCT_ENTERPRISE_N As Integer = &H1B
Private Const PRODUCT_ULTIMATE_N As Integer = &H1C
Private Const PRODUCT_WEB_SERVER_CORE As Integer = &H1D
Private Const PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT As Integer = &H1E
Private Const PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY As Integer = &H1F
Private Const PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING As Integer = &H20
Private Const PRODUCT_SERVER_FOUNDATION As Integer = &H21
Private Const PRODUCT_HOME_PREMIUM_SERVER As Integer = &H22
Private Const PRODUCT_SERVER_FOR_SMALLBUSINESS_V As Integer = &H23
Private Const PRODUCT_STANDARD_SERVER_V As Integer = &H24
Private Const PRODUCT_DATACENTER_SERVER_V As Integer = &H25
Private Const PRODUCT_ENTERPRISE_SERVER_V As Integer = &H26
Private Const PRODUCT_DATACENTER_SERVER_CORE_V As Integer = &H27
Private Const PRODUCT_STANDARD_SERVER_CORE_V As Integer = &H28
Private Const PRODUCT_ENTERPRISE_SERVER_CORE_V As Integer = &H29
Private Const PRODUCT_HYPERV As Integer = &H2A
Private Const PRODUCT_STORAGE_EXPRESS_SERVER_CORE As Integer = &H2B
Private Const PRODUCT_STORAGE_STANDARD_SERVER_CORE As Integer = &H2C
Private Const PRODUCT_STORAGE_WORKGROUP_SERVER_CORE As Integer = &H2D
Private Const PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE As Integer = &H2E
Private Const PRODUCT_STARTER_N As Integer = &H2F
Private Const PRODUCT_PROFESSIONAL As Integer = &H30
Private Const PRODUCT_PROFESSIONAL_N As Integer = &H31
Private Const PRODUCT_SB_SOLUTION_SERVER As Integer = &H32
Private Const PRODUCT_SERVER_FOR_SB_SOLUTIONS As Integer = &H33
Private Const PRODUCT_STANDARD_SERVER_SOLUTIONS As Integer = &H34
Private Const PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE As Integer = &H35
Private Const PRODUCT_SB_SOLUTION_SERVER_EM As Integer = &H36
Private Const PRODUCT_SERVER_FOR_SB_SOLUTIONS_EM As Integer = &H37
Private Const PRODUCT_SOLUTION_EMBEDDEDSERVER As Integer = &H38
Private Const PRODUCT_SOLUTION_EMBEDDEDSERVER_CORE As Integer = &H39
'private const int ???? = 0x0000003A;
Private Const PRODUCT_ESSENTIALBUSINESS_SERVER_MGMT As Integer = &H3B
Private Const PRODUCT_ESSENTIALBUSINESS_SERVER_ADDL As Integer = &H3C
Private Const PRODUCT_ESSENTIALBUSINESS_SERVER_MGMTSVC As Integer = &H3D
Private Const PRODUCT_ESSENTIALBUSINESS_SERVER_ADDLSVC As Integer = &H3E
Private Const PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE As Integer = &H3F
Private Const PRODUCT_CLUSTER_SERVER_V As Integer = &H40
Private Const PRODUCT_EMBEDDED As Integer = &H41
Private Const PRODUCT_STARTER_E As Integer = &H42
Private Const PRODUCT_HOME_BASIC_E As Integer = &H43
Private Const PRODUCT_HOME_PREMIUM_E As Integer = &H44
Private Const PRODUCT_PROFESSIONAL_E As Integer = &H45
Private Const PRODUCT_ENTERPRISE_E As Integer = &H46
Private Const PRODUCT_ULTIMATE_E As Integer = &H47
'private const int PRODUCT_UNLICENSED = 0xABCDABCD;
#End Region

#Region "VERSIONS"
Private Const VER_NT_WORKSTATION As Integer = 1
Private Const VER_NT_DOMAIN_CONTROLLER As Integer = 2
Private Const VER_NT_SERVER As Integer = 3
Private Const VER_SUITE_SMALLBUSINESS As Integer = 1
Private Const VER_SUITE_ENTERPRISE As Integer = 2
Private Const VER_SUITE_TERMINAL As Integer = 16
Private Const VER_SUITE_DATACENTER As Integer = 128
Private Const VER_SUITE_SINGLEUSERTS As Integer = 256
Private Const VER_SUITE_PERSONAL As Integer = 512
Private Const VER_SUITE_BLADE As Integer = 1024
#End Region

#End Region

#Region "DLLImports"

Private Declare Sub GetNativeSystemInfo Lib "kernel32" (<MarshalAs(UnmanagedType.Struct)> ByRef lpSystemInfo As SYSTEM_INFO)

<DllImport("kernel32.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi)> _
Private Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal procName As String) As IntPtr
End Function

<DllImport("Kernel32.dll")> _
Private Shared Function GetProductInfo(ByVal osMajorVersion As Integer, ByVal osMinorVersion As Integer, ByVal spMajorVersion As Integer, ByVal spMinorVersion As Integer, ByRef edition As Integer) As Boolean
End Function

Private Declare Auto Function GetSystemMetrics Lib "user32.dll" (ByVal smIndex As Integer) As Integer

<DllImport("kernel32.dll")> _
Private Shared Function GetVersionEx(ByRef osVersionInfo As OSVERSIONINFOEX) As Boolean
End Function

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

<DllImport("kernel32.dll", SetLastError:=True, CallingConvention:=CallingConvention.Winapi)> _
Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
#End Region

#Region "Delegates"

Private Delegate Function IsWow64ProcessDelegate(ByVal handle As IntPtr, ByRef isWow64Process As Boolean) As Boolean

#End Region


#Region "Properties"

#Region "Name"
Private _name As String
''' <summary>
''' Gets the name of the operating system running on this computer.
''' </summary>
Public ReadOnly Property Name() As String
Get
If _name IsNot Nothing Then
Return _name
End If

Dim sName As String = "unknown"
Dim osVersion As OperatingSystem = Environment.OSVersion
Dim osVersionInfo As New OSVERSIONINFOEX()
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(GetType(OSVERSIONINFOEX))

If GetVersionEx(osVersionInfo) Then
Dim majorVersion As Integer = osVersion.Version.Major
Dim minorVersion As Integer = osVersion.Version.Minor

Select Case osVersion.Platform
Case PlatformID.Win32S
sName = "Windows 3.1"
Exit Select
Case PlatformID.WinCE
sName = "Windows CE"
Exit Select
Case PlatformID.Win32Windows
If True Then
If majorVersion = 4 Then
Dim csdVersion As String = osVersionInfo.szCSDVersion
Select Case minorVersion
Case 0
If csdVersion = "B" OrElse csdVersion = "C" Then
sName = "Windows 95 OSR2"
Else
sName = "Windows 95"
End If
Exit Select
Case 10
If csdVersion = "A" Then
sName = "Windows 98 Second Edition"
Else
sName = "Windows 98"
End If
Exit Select
Case 90
sName = "Windows Me"
Exit Select
End Select
End If
Exit Select
End If
Case PlatformID.Win32NT
If True Then
Dim productType As Byte = osVersionInfo.wProductType

Select Case majorVersion
Case 3
sName = "Windows NT 3.51"
Exit Select
Case 4
Select Case productType
Case 1
sName = "Windows NT 4.0"
Exit Select
Case 3
sName = "Windows NT 4.0 Server"
Exit Select
End Select
Exit Select
Case 5
Select Case minorVersion
Case 0
sName = "Windows 2000"
Exit Select
Case 1
sName = "Windows XP"
Exit Select
Case 2
sName = "Windows Server 2003"
Exit Select
End Select
Exit Select
Case 6
Select Case minorVersion
Case 0
Select Case productType
Case 1
sName = "Windows Vista"
Exit Select
Case 3
sName = "Windows Server 2008"
Exit Select
End Select
Exit Select

Case 1
Select Case productType
Case 1
sName = "Windows 7"
Exit Select
Case 3
sName = "Windows Server 2008 R2"
Exit Select
End Select
Exit Select
End Select
Exit Select
End Select
Exit Select
End If
End Select
End If

_name = sName
Return sName
End Get
End Property

#End Region

#Region "Edition"
Private _edition As String
''' <summary>
''' Gets the edition of the operating system running on this computer.
''' </summary>
Public ReadOnly Property Edition() As String
Get
If _edition IsNot Nothing Then
Return _edition
End If

Dim sEdition As String = String.Empty
Dim osVersion As OperatingSystem = Environment.OSVersion
Dim osVersionInfo As New OSVERSIONINFOEX()
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(GetType(OSVERSIONINFOEX))

If GetVersionEx(osVersionInfo) Then
Dim majorVersion As Integer = osVersion.Version.Major
Dim minorVersion As Integer = osVersion.Version.Minor
Dim productType As Byte = osVersionInfo.wProductType
Dim suiteMask As Short = osVersionInfo.wSuiteMask

'#Region "VERSION 4"
If majorVersion = 4 Then
If productType = VER_NT_WORKSTATION Then
' Windows NT 4.0 Workstation
sEdition = "Workstation"
ElseIf productType = VER_NT_SERVER Then
If (suiteMask And VER_SUITE_ENTERPRISE) <> 0 Then
' Windows NT 4.0 Server Enterprise
sEdition = "Enterprise Server"
Else
' Windows NT 4.0 Server
sEdition = "Standard Server"
End If
End If
'#End Region

'#Region "VERSION 5"
ElseIf majorVersion = 5 Then
If productType = VER_NT_WORKSTATION Then
If (suiteMask And VER_SUITE_PERSONAL) <> 0 Then
sEdition = "Home"
Else
If GetSystemMetrics(86) = 0 Then
' 86 == SM_TABLETPC
sEdition = "Professional"
Else
sEdition = "Tablet Edition"
End If
End If
ElseIf productType = VER_NT_SERVER Then
If minorVersion = 0 Then
If (suiteMask And VER_SUITE_DATACENTER) <> 0 Then
' Windows 2000 Datacenter Server
sEdition = "Datacenter Server"
ElseIf (suiteMask And VER_SUITE_ENTERPRISE) <> 0 Then
' Windows 2000 Advanced Server
sEdition = "Advanced Server"
Else
' Windows 2000 Server
sEdition = "Server"
End If
Else
If (suiteMask And VER_SUITE_DATACENTER) <> 0 Then
' Windows Server 2003 Datacenter Edition
sEdition = "Datacenter"
ElseIf (suiteMask And VER_SUITE_ENTERPRISE) <> 0 Then
' Windows Server 2003 Enterprise Edition
sEdition = "Enterprise"
ElseIf (suiteMask And VER_SUITE_BLADE) <> 0 Then
' Windows Server 2003 Web Edition
sEdition = "Web Edition"
Else
' Windows Server 2003 Standard Edition
sEdition = "Standard"
End If
End If
End If
'#End Region

'#Region "VERSION 6"
ElseIf majorVersion = 6 Then
Dim ed As Integer
If GetProductInfo(majorVersion, minorVersion, osVersionInfo.wServicePackMajor, osVersionInfo.wServicePackMinor, ed) Then
Select Case ed
Case PRODUCT_BUSINESS
sEdition = "Business"
Exit Select
Case PRODUCT_BUSINESS_N
sEdition = "Business N"
Exit Select
Case PRODUCT_CLUSTER_SERVER
sEdition = "HPC Edition"
Exit Select
Case PRODUCT_CLUSTER_SERVER_V
sEdition = "HPC Edition without Hyper-V"
Exit Select
Case PRODUCT_DATACENTER_SERVER
sEdition = "Datacenter Server"
Exit Select
Case PRODUCT_DATACENTER_SERVER_CORE
sEdition = "Datacenter Server (core installation)"
Exit Select
Case PRODUCT_DATACENTER_SERVER_V
sEdition = "Datacenter Server without Hyper-V"
Exit Select
Case PRODUCT_DATACENTER_SERVER_CORE_V
sEdition = "Datacenter Server without Hyper-V (core installation)"
Exit Select
Case PRODUCT_EMBEDDED
sEdition = "Embedded"
Exit Select
Case PRODUCT_ENTERPRISE
sEdition = "Enterprise"
Exit Select
Case PRODUCT_ENTERPRISE_N
sEdition = "Enterprise N"
Exit Select
Case PRODUCT_ENTERPRISE_E
sEdition = "Enterprise E"
Exit Select
Case PRODUCT_ENTERPRISE_SERVER
sEdition = "Enterprise Server"
Exit Select
Case PRODUCT_ENTERPRISE_SERVER_CORE
sEdition = "Enterprise Server (core installation)"
Exit Select
Case PRODUCT_ENTERPRISE_SERVER_CORE_V
sEdition = "Enterprise Server without Hyper-V (core installation)"
Exit Select
Case PRODUCT_ENTERPRISE_SERVER_IA64
sEdition = "Enterprise Server for Itanium-based Systems"
Exit Select
Case PRODUCT_ENTERPRISE_SERVER_V
sEdition = "Enterprise Server without Hyper-V"
Exit Select
Case PRODUCT_ESSENTIALBUSINESS_SERVER_MGMT
sEdition = "Essential Business Server MGMT"
Exit Select
Case PRODUCT_ESSENTIALBUSINESS_SERVER_ADDL
sEdition = "Essential Business Server ADDL"
Exit Select
Case PRODUCT_ESSENTIALBUSINESS_SERVER_MGMTSVC
sEdition = "Essential Business Server MGMTSVC"
Exit Select
Case PRODUCT_ESSENTIALBUSINESS_SERVER_ADDLSVC
sEdition = "Essential Business Server ADDLSVC"
Exit Select
Case PRODUCT_HOME_BASIC
sEdition = "Home Basic"
Exit Select
Case PRODUCT_HOME_BASIC_N
sEdition = "Home Basic N"
Exit Select
Case PRODUCT_HOME_BASIC_E
sEdition = "Home Basic E"
Exit Select
Case PRODUCT_HOME_PREMIUM
sEdition = "Home Premium"
Exit Select
Case PRODUCT_HOME_PREMIUM_N
sEdition = "Home Premium N"
Exit Select
Case PRODUCT_HOME_PREMIUM_E
sEdition = "Home Premium E"
Exit Select
Case PRODUCT_HOME_PREMIUM_SERVER
sEdition = "Home Premium Server"
Exit Select
Case PRODUCT_HYPERV
sEdition = "Microsoft Hyper-V Server"
Exit Select
Case PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT
sEdition = "Windows Essential Business Management Server"
Exit Select
Case PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING
sEdition = "Windows Essential Business Messaging Server"
Exit Select
Case PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY
sEdition = "Windows Essential Business Security Server"
Exit Select
Case PRODUCT_PROFESSIONAL
sEdition = "Professional"
Exit Select
Case PRODUCT_PROFESSIONAL_N
sEdition = "Professional N"
Exit Select
Case PRODUCT_PROFESSIONAL_E
sEdition = "Professional E"
Exit Select
Case PRODUCT_SB_SOLUTION_SERVER
sEdition = "SB Solution Server"
Exit Select
Case PRODUCT_SB_SOLUTION_SERVER_EM
sEdition = "SB Solution Server EM"
Exit Select
Case PRODUCT_SERVER_FOR_SB_SOLUTIONS
sEdition = "Server for SB Solutions"
Exit Select
Case PRODUCT_SERVER_FOR_SB_SOLUTIONS_EM
sEdition = "Server for SB Solutions EM"
Exit Select
Case PRODUCT_SERVER_FOR_SMALLBUSINESS
sEdition = "Windows Essential Server Solutions"
Exit Select
Case PRODUCT_SERVER_FOR_SMALLBUSINESS_V
sEdition = "Windows Essential Server Solutions without Hyper-V"
Exit Select
Case PRODUCT_SERVER_FOUNDATION
sEdition = "Server Foundation"
Exit Select
Case PRODUCT_SMALLBUSINESS_SERVER
sEdition = "Windows Small Business Server"
Exit Select
Case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM
sEdition = "Windows Small Business Server Premium"
Exit Select
Case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE
sEdition = "Windows Small Business Server Premium (core installation)"
Exit Select
Case PRODUCT_SOLUTION_EMBEDDEDSERVER
sEdition = "Solution Embedded Server"
Exit Select
Case PRODUCT_SOLUTION_EMBEDDEDSERVER_CORE
sEdition = "Solution Embedded Server (core installation)"
Exit Select
Case PRODUCT_STANDARD_SERVER
sEdition = "Standard Server"
Exit Select
Case PRODUCT_STANDARD_SERVER_CORE
sEdition = "Standard Server (core installation)"
Exit Select
Case PRODUCT_STANDARD_SERVER_SOLUTIONS
sEdition = "Standard Server Solutions"
Exit Select
Case PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE
sEdition = "Standard Server Solutions (core installation)"
Exit Select
Case PRODUCT_STANDARD_SERVER_CORE_V
sEdition = "Standard Server without Hyper-V (core installation)"
Exit Select
Case PRODUCT_STANDARD_SERVER_V
sEdition = "Standard Server without Hyper-V"
Exit Select
Case PRODUCT_STARTER
sEdition = "Starter"
Exit Select
Case PRODUCT_STARTER_N
sEdition = "Starter N"
Exit Select
Case PRODUCT_STARTER_E
sEdition = "Starter E"
Exit Select
Case PRODUCT_STORAGE_ENTERPRISE_SERVER
sEdition = "Enterprise Storage Server"
Exit Select
Case PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE
sEdition = "Enterprise Storage Server (core installation)"
Exit Select
Case PRODUCT_STORAGE_EXPRESS_SERVER
sEdition = "Express Storage Server"
Exit Select
Case PRODUCT_STORAGE_EXPRESS_SERVER_CORE
sEdition = "Express Storage Server (core installation)"
Exit Select
Case PRODUCT_STORAGE_STANDARD_SERVER
sEdition = "Standard Storage Server"
Exit Select
Case PRODUCT_STORAGE_STANDARD_SERVER_CORE
sEdition = "Standard Storage Server (core installation)"
Exit Select
Case PRODUCT_STORAGE_WORKGROUP_SERVER
sEdition = "Workgroup Storage Server"
Exit Select
Case PRODUCT_STORAGE_WORKGROUP_SERVER_CORE
sEdition = "Workgroup Storage Server (core installation)"
Exit Select
Case PRODUCT_UNDEFINED
sEdition = "Unknown product"
Exit Select
Case PRODUCT_ULTIMATE
sEdition = "Ultimate"
Exit Select
Case PRODUCT_ULTIMATE_N
sEdition = "Ultimate N"
Exit Select
Case PRODUCT_ULTIMATE_E
sEdition = "Ultimate E"
Exit Select
Case PRODUCT_WEB_SERVER
sEdition = "Web Server"
Exit Select
Case PRODUCT_WEB_SERVER_CORE
sEdition = "Web Server (core installation)"
Exit Select
End Select
End If
'#End Region
End If
End If

_edition = sEdition
Return sEdition
End Get
End Property
#End Region

#Region "VersionInfo"

#Region "BuildVersion"
''' <summary>
''' Gets the build version number of the operating system running on this computer.
''' </summary>
Public ReadOnly Property BuildVersion() As Integer
Get
Return Environment.OSVersion.Version.Build
End Get
End Property

#End Region

#Region "VersionString"
''' <summary>
''' Gets the full version string of the operating system running on this computer.
''' </summary>
Public ReadOnly Property VersionString() As String
Get
Return Environment.OSVersion.Version.ToString()
End Get
End Property
#End Region

#Region "Version"
''' <summary>
''' Gets the full version of the operating system running on this computer.
''' </summary>
Public ReadOnly Property Version() As Version
Get
Return Environment.OSVersion.Version
End Get
End Property
#End Region

#Region "MajorVersion"
''' <summary>
''' Gets the major version number of the operating system running on this computer.
''' </summary>
Public ReadOnly Property MajorVersion() As Integer
Get
Return Environment.OSVersion.Version.Major
End Get
End Property
#End Region

#Region "MinorVersion"
''' <summary>
''' Gets the minor version number of the operating system running on this computer.
''' </summary>
Public ReadOnly Property MinorVersion() As Integer
Get
Return Environment.OSVersion.Version.Minor
End Get
End Property
#End Region

#Region "Revision"
''' <summary>
''' Gets the revision version number of the operating system running on this computer.
''' </summary>
Public ReadOnly Property RevisionVersion() As Integer
Get
Return Environment.OSVersion.Version.Revision
End Get
End Property
#End Region

#Region "ServicePack"
''' <summary>
''' Gets the service pack information of the operating system running on this computer.
''' </summary>
Public ReadOnly Property ServicePack() As String
Get
Dim sp As String = String.Empty
Dim osVersionInfo As New OSVERSIONINFOEX()

osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(GetType(OSVERSIONINFOEX))

If GetVersionEx(osVersionInfo) Then
sp = osVersionInfo.szCSDVersion
End If

Return sp
End Get
End Property
#End Region

#End Region

#Region "Bits"

#Region "OSBits"
''' <summary>
''' Determines if the current operating system is 32 or 64-bit.
''' </summary>
Public ReadOnly Property OSBits() As SoftwareArchitecture
Get
Dim ret As SoftwareArchitecture = SoftwareArchitecture.Unknown

Select Case IntPtr.Size * 8
Case 64
ret = SoftwareArchitecture.Bit64
Exit Select

Case 32
If Is32BitProcessOn64BitProcessor() Then
ret = SoftwareArchitecture.Bit64
Else
ret = SoftwareArchitecture.Bit32
End If
Exit Select

Case Else
ret = SoftwareArchitecture.Unknown
Exit Select
End Select

Return ret
End Get
End Property

#End Region

#Region "ProgramBits"
''' <summary>
''' Determines if the current application is 32 or 64-bit.
''' </summary>
Public ReadOnly Property ProgramBits() As SoftwareArchitecture
Get
Dim pbits As SoftwareArchitecture = SoftwareArchitecture.Unknown

Dim test As System.Collections.IDictionary = Environment.GetEnvironmentVariables()

Select Case IntPtr.Size * 8
Case 64
pbits = SoftwareArchitecture.Bit64
Exit Select

Case 32
pbits = SoftwareArchitecture.Bit32
Exit Select
Case Else

pbits = SoftwareArchitecture.Unknown
Exit Select
End Select

Return pbits
End Get
End Property

#End Region

#Region "ProcessorBits"
''' <summary>
''' Determines if the current processor is 32 or 64-bit.
''' </summary>
Public ReadOnly Property ProcessorBits() As ProcessorArchitecture
Get
Dim pbits As ProcessorArchitecture = ProcessorArchitecture.Unknown

Try
Dim l_System_Info As New SYSTEM_INFO()
GetNativeSystemInfo(l_System_Info)

Select Case l_System_Info.uProcessorInfo.wProcessorArchitecture
Case 9
' PROCESSOR_ARCHITECTURE_AMD64
pbits = ProcessorArchitecture.Bit64
Exit Select
Case 6
' PROCESSOR_ARCHITECTURE_IA64
pbits = ProcessorArchitecture.Itanium64
Exit Select
Case 0
' PROCESSOR_ARCHITECTURE_INTEL
pbits = ProcessorArchitecture.Bit32
Exit Select
Case Else
' PROCESSOR_ARCHITECTURE_UNKNOWN
pbits = ProcessorArchitecture.Unknown
Exit Select
End Select
' Ignore
Catch
End Try

Return pbits
End Get
End Property

#End Region

#End Region

#End Region


#Region "Functions"

Private Function Is32BitProcessOn64BitProcessor() As Boolean
Dim fnDelegate As IsWow64ProcessDelegate = GetIsWow64ProcessDelegate()

If fnDelegate Is Nothing Then
Return False
End If

Dim isWow64 As Boolean
Dim retVal As Boolean = fnDelegate.Invoke(Process.GetCurrentProcess().Handle, isWow64)

If retVal = False Then
Return False
End If

Return isWow64
End Function

Private Function GetIsWow64ProcessDelegate() As IsWow64ProcessDelegate
Dim handle As IntPtr = LoadLibrary("kernel32")

If handle <> IntPtr.Zero Then
Dim fnPtr As IntPtr = GetProcAddress(handle, "IsWow64Process")
If fnPtr <> IntPtr.Zero Then
Return DirectCast(Marshal.GetDelegateForFunctionPointer(DirectCast(fnPtr, IntPtr), GetType(IsWow64ProcessDelegate)), IsWow64ProcessDelegate)
End If
End If

Return Nothing
End Function

Public Overrides Function ToString() As String
Dim sb As New StringBuilder([String].Empty)

sb.AppendLine(String.Format("Name = {0}", Me.Name))
sb.AppendLine(String.Format("Edition = {0}", Me.Edition))
If Me.ServicePack <> String.Empty Then
sb.AppendLine(String.Format("Service Pack = {0}", Me.ServicePack))
Else
sb.AppendLine("Service Pack = None")
End If
sb.AppendLine(String.Format("Version = {0}", Me.VersionString))
sb.AppendLine(String.Format("ProcessorBits = {0}", Me.ProcessorBits))
sb.AppendLine(String.Format("OSBits = {0}", Me.OSBits))
sb.AppendLine(String.Format("ProgramBits = {0}", Me.ProgramBits))

Return sb.ToString()
End Function

#End Region

End Class
09.02.2011
Code-Tag hinzugefügt
Konstantin 09.02.2011

Stelle deine .net-Frage jetzt!
TOP TECHNOLOGIES CONSULTING GmbH