using System;
using System.Runtime.InteropServices;
class Program
{
[STAThread]
static void Main(string[] args)
{
// GetVersionEx関数によりOSの情報を取得
OSVERSIONINFOEX osInfo = new OSVERSIONINFOEX();
osInfo.dwOSVersionInfoSize = OSVERSIONINFOEX_SIZE;
int bVersionEx = GetVersionEx(ref osInfo);
if (bVersionEx == 0)
{
osInfo.dwOSVersionInfoSize = OSVERSIONINFO_SIZE;
GetVersionEx(ref osInfo);
}
string windowsName = "Unknown Windows"; // Windows名
switch (osInfo.dwPlatformId)
{
case VER_PLATFORM_WIN32_WINDOWS: // Windows 9x系
if (osInfo.dwMajorVersion == 4)
{
switch (osInfo.dwMinorVersion)
{
case 0: // .NET Frameworkのサポートなし
windowsName = "Windows 95";
break;
case 10:
;
if ((osInfo.szCSDVersion.Length <= 0) ||
(osInfo.szCSDVersion.Replace(" ", "") != "A"))
{
windowsName = "Windows 98";
}
else
{
windowsName = "Windows 98 Second Edition";
}
break;
case 90:
windowsName = "Windows Me";
break;
}
}
break;
case VER_PLATFORM_WIN32_NT: // Windows NT系
if (osInfo.dwMajorVersion == 4)
{
// .NET Framework 2.0以降のサポートなし
windowsName = "Windows NT 4.0";
// ※判定処理を省略
}
else if (osInfo.dwMajorVersion == 5)
{
switch (osInfo.dwMinorVersion)
{
case 0:
windowsName = "Windows 2000";
if ((osInfo.wSuiteMask & VER_SUITE_DATACENTER)
== VER_SUITE_DATACENTER)
{
windowsName += " Datacenter Server";
}
else if ((osInfo.wSuiteMask & VER_SUITE_ENTERPRISE)
== VER_SUITE_ENTERPRISE)
{
windowsName += " Advanced Server";
}
else
{
windowsName += " Server";
}
break;
case 1:
windowsName = "Windows XP";
if ((osInfo.wSuiteMask & VER_SUITE_PERSONAL)
== VER_SUITE_PERSONAL)
{
windowsName += " Home Edition";
}
else
{
windowsName += " Professional";
}
break;
case 2:
windowsName = "Windows Server 2003";
if ((osInfo.wSuiteMask & VER_SUITE_DATACENTER)
== VER_SUITE_DATACENTER)
{
windowsName += " Datacenter Edition";
}
else if ((osInfo.wSuiteMask & VER_SUITE_ENTERPRISE)
== VER_SUITE_ENTERPRISE)
{
windowsName += " Enterprise Edition";
}
else if (osInfo.wSuiteMask == VER_SUITE_BLADE)
{
windowsName += " Web Edition";
}
else
{
windowsName += " Standard Edition";
}
break;
}
}
else if (osInfo.dwMajorVersion == 6)
{
switch (osInfo.dwMinorVersion)
{
case 0:
windowsName = "Windows Vista";
break;
}
// Vistaの場合にはGetProductInfo関数を使用して
// Edition情報を取得する
uint edition = PRODUCT_UNDEFINED;
if (GetProductInfo(
osInfo.dwMajorVersion,
osInfo.dwMinorVersion,
osInfo.wServicePackMajor,
osInfo.wServicePackMinor,
out edition))
{
switch (edition)
{
case PRODUCT_ENTERPRISE:
windowsName += " Enterprise Edition";
break;
case PRODUCT_ULTIMATE:
windowsName += " Ultimate Edition";
break;
case PRODUCT_BUSINESS:
windowsName += " Business Edition";
break;
case PRODUCT_HOME_PREMIUM:
windowsName += " Home Premium Edition";
break;
case PRODUCT_HOME_BASIC:
windowsName += " Home Basic Edition";
break;
default:
windowsName += " Unknown Edition";
break;
}
}
}
break;
}
string strPlatform = "Unknown Windows";
if (osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
strPlatform = "Win32Windows";
}
else if (osInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
strPlatform = "Win32NT";
}
// システム情報を出力
Console.WriteLine(
"{0} (Platform {1} Version {2}.{3} Build {4}) {5}",
windowsName,
strPlatform,
osInfo.dwMajorVersion,
osInfo.dwMinorVersion,
osInfo.dwBuildNumber,
osInfo.szCSDVersion);
}
// GetVersionEx関数を使うための定義
[StructLayout(LayoutKind.Sequential)]
public struct OSVERSIONINFOEX
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
public byte wProductType;
public byte wReserved;
}
[DllImport("kernel32.dll", EntryPoint="GetVersionExA")]
public static extern int GetVersionEx(ref OSVERSIONINFOEX o);
// OSVERSIONINFOEX構造体サイズ
public const short OSVERSIONINFOEX_SIZE = 156;
public const short OSVERSIONINFO_SIZE = 148;
// dwPlatformId定義値
public const byte VER_PLATFORM_WIN32s = 0;
public const byte VER_PLATFORM_WIN32_WINDOWS = 1;
public const byte VER_PLATFORM_WIN32_NT = 2;
// wSuiteMask定義値
public const short VER_SUITE_PERSONAL = 0x0200; // Windows XP Home Edition
public const short VER_SUITE_DATACENTER = 0x0080; // Windows 2000 Datacenter Server, or Windows Server 2003, Datacenter Edition
public const short VER_SUITE_ENTERPRISE = 0x0002; // Windows NT 4.0 Enterprise Edition or Windows 2000 Advanced Server, or Windows Server 2003, Enterprise Edition
public const short VER_SUITE_BLADE = 0x0400; // Windows Server 2003, Web Edition
[DllImport("Kernel32.dll")]
public static extern bool GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
out uint pdwReturnedProductType);
// pdwReturnedProductType定義値
public const uint PRODUCT_BUSINESS= 0x00000006; // Business Edition
public const uint PRODUCT_ENTERPRISE = 0x00000004; // Enterprise Edition
public const uint PRODUCT_HOME_BASIC = 0x00000002; // Home Basic Edition
public const uint PRODUCT_HOME_PREMIUM = 0x00000003; // Home Premium Edition
public const uint PRODUCT_ULTIMATE = 0x00000001; // Ultimate Edition
public const uint PRODUCT_UNDEFINED = 0x00000000; // An unknown product
}
|
Imports System.Runtime.InteropServices
Module Module1
Sub Main()
' GetVersionEx関数によりOSの情報を取得
Dim osInfo As New OSVERSIONINFOEX '
osInfo.dwOSVersionInfoSize = OSVERSIONINFOEX_SIZE
Dim bVersionEx As Integer = GetVersionEx(osInfo)
If bVersionEx = 0 Then
osInfo.dwOSVersionInfoSize = OSVERSIONINFO_SIZE
GetVersionEx(osInfo)
End If
Dim windowsName As String = "Unknown Windows" ' Windows名
Select Case osInfo.dwPlatformId
Case VER_PLATFORM_WIN32_WINDOWS ' Windows 9x系
If osInfo.dwMajorVersion = 4 Then
Select Case osInfo.dwMinorVersion
Case 0 ' .NET Frameworkのサポートなし
windowsName = "Windows 95"
Case 10
If ((osInfo.szCSDVersion.Length <= 0) Or _
(osInfo.szCSDVersion.Replace(" ", "") <> "A")) Then
windowsName = "Windows 98"
Else
windowsName = "Windows 98 Second Edition"
End If
Case 90
windowsName = "Windows Me"
End Select
End If
Case VER_PLATFORM_WIN32_NT ' Windows NT系
If osInfo.dwMajorVersion = 4 Then
' .NET Framework 2.0以降のサポートなし
windowsName = "Windows NT 4.0"
' ※判定処理を省略
ElseIf osInfo.dwMajorVersion = 5 Then
Select Case osInfo.dwMinorVersion
Case 0
windowsName = "Windows 2000"
If (osInfo.wSuiteMask And VER_SUITE_DATACENTER) _
= VER_SUITE_DATACENTER Then
windowsName += " Datacenter Server"
ElseIf (osInfo.wSuiteMask And VER_SUITE_ENTERPRISE) _
= VER_SUITE_ENTERPRISE Then
windowsName += " Advanced Server"
Else
windowsName += " Server"
End If
Case 1
windowsName = "Windows XP"
If (osInfo.wSuiteMask And VER_SUITE_PERSONAL) _
= VER_SUITE_PERSONAL Then
windowsName += " Home Edition"
Else
windowsName += " Professional"
End If
Case 2
windowsName = "Windows Server 2003"
If (osInfo.wSuiteMask And VER_SUITE_DATACENTER) _
= VER_SUITE_DATACENTER Then
windowsName += " Datacenter Edition"
ElseIf (osInfo.wSuiteMask And VER_SUITE_ENTERPRISE) _
= VER_SUITE_ENTERPRISE Then
windowsName += " Enterprise Edition"
ElseIf osInfo.wSuiteMask = VER_SUITE_BLADE Then
windowsName += " Web Edition"
Else
windowsName += " Standard Edition"
End If
End Select
ElseIf osInfo.dwMajorVersion = 6 Then
Select Case osInfo.dwMinorVersion
Case 0
windowsName = "Windows Vista"
End Select
' Vistaの場合にはGetProductInfo関数を使用して
' Edition情報を取得する
Dim edition As UInteger = PRODUCT_UNDEFINED
If GetProductInfo( _
osInfo.dwMajorVersion, _
osInfo.dwMinorVersion, _
osInfo.wServicePackMajor, _
osInfo.wServicePackMinor, _
edition) Then
Select Case edition
Case PRODUCT_ENTERPRISE
windowsName += " Enterprise Edition"
Case PRODUCT_ULTIMATE
windowsName += " Ultimate Edition"
Case PRODUCT_BUSINESS
windowsName += " Business Edition"
Case PRODUCT_HOME_PREMIUM
windowsName += " Home Premium Edition"
Case PRODUCT_HOME_BASIC
windowsName += " Home Basic Edition"
Case Else
windowsName += " Unknown Edition"
End Select
End If
End If
End Select
Dim strPlatform As String = "Unknown Windows"
If osInfo.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
strPlatform = "Win32Windows"
ElseIf osInfo.dwPlatformId = VER_PLATFORM_WIN32_NT Then
strPlatform = "Win32NT"
End If
' システム情報を出力
Console.WriteLine( _
"{0} (Platform {1} Version {2}.{3} Build {4}) {5}", _
windowsName, strPlatform, _
osInfo.dwMajorVersion, _
osInfo.dwMinorVersion, _
osInfo.dwBuildNumber, _
osInfo.szCSDVersion)
End Sub 'Main
' GetVersionEx関数を使うための定義
<StructLayout(LayoutKind.Sequential)> _
Public Structure OSVERSIONINFOEX
Dim dwOSVersionInfoSize As Integer
Dim dwMajorVersion As Integer
Dim dwMinorVersion As Integer
Dim dwBuildNumber As Integer
Dim dwPlatformId As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Public szCSDVersion As String
Dim wServicePackMajor As Short
Dim wServicePackMinor As Short
Dim wSuiteMask As Short
Dim wProductType As Byte
Dim wReserved As Byte
End Structure
Public Declare Function GetVersionEx Lib "Kernel32.dll" Alias "GetVersionExA" (ByRef o As OSVERSIONINFOEX) As Integer
' OSVERSIONINFOEX構造体サイズ
Public Const OSVERSIONINFOEX_SIZE As Short = 156
Public Const OSVERSIONINFO_SIZE As Short = 148
' dwPlatformId定義値
Public Const VER_PLATFORM_WIN32s As Byte = 0
Public Const VER_PLATFORM_WIN32_WINDOWS As Byte = 1
Public Const VER_PLATFORM_WIN32_NT As Byte = 2
' wSuiteMask定義値
Public Const VER_SUITE_PERSONAL As Short = &H200 ' Windows XP Home Edition
Public Const VER_SUITE_DATACENTER As Short = &H80 ' Windows 2000 Datacenter Serve, or Windows Server 2003, Datacenter Edition
Public Const VER_SUITE_ENTERPRISE As Short = &H2 ' Windows NT 4.0 Enterprise Edition, or Windows 2000 Advanced Server, or Windows Server 2003, Enterprise Edition
Public Const VER_SUITE_BLADE As Short = &H400 ' Windows Server 2003, Web Edition
Public Declare Function GetProductInfo Lib "Kernel32.dll" (ByVal dwOSMajorVersion As Integer, ByVal dwOSMinorVersion As Integer, ByVal dwSpMajorVersion As Integer, ByVal dwSpMinorVersion As Integer, <Out()> ByRef pdwReturnedProductType As UInteger) As Boolean
' pdwReturnedProductType定義値
Public Const PRODUCT_BUSINESS As UInteger = &H6 ' Business Edition
Public Const PRODUCT_ENTERPRISE As UInteger = &H4 ' Enterprise Edition
Public Const PRODUCT_HOME_BASIC As UInteger = &H2 ' Home Basic Edition
Public Const PRODUCT_HOME_PREMIUM As UInteger = &H3 ' Home Premium Edition
Public Const PRODUCT_ULTIMATE As UInteger = &H1 ' Ultimate Edition
Public Const PRODUCT_UNDEFINED As UInteger = &H0 ' An unknown product
End Module
|
|