Skip to content

Instantly share code, notes, and snippets.

@fmeeen

fmeeen/MdlOs.vb

Created Oct 9, 2013
Embed
What would you like to do?
OSバージョン取得モジュール
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Module MdlOs
'GetVersionExを使うための定義
<StructLayout(LayoutKind.Sequential)> _
Private 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
Private Declare Function GetVersionEx Lib "kernel32.dll" Alias "GetVersionExA" (ByRef o As OSVERSIONINFOEX) As Integer
'OSVERSIONINFOEX構造体サイズ
Private Const OSVERSIONINFOEX_SIZE As Short = 156
Private Const OSVERSIONINFO_SIZE As Short = 148
'dwPlatformId定義値
Private Const VER_PLATFORM_WIN32s As Byte = 0 'MS-DOS系
Private Const VER_PLATFORM_WIN32_WINDOWS As Byte = 1 'Windows 9x系
Private Const VER_PLATFORM_WIN32_NT As Byte = 2 'Windows NT系
'wSuiteMask定義値
Private Const VER_SUITE_PERSONAL As Short = &H200 'Windows Vista Home Premium, Windows Vista Home Basic, or Windows XP Home Edition
Private Const VER_SUITE_DATACENTER As Short = &H80 'Windows Server 2008 Datacenter, Windows Server 2003, Datacenter Edition, or Windows 2000 Datacenter Server
Private Const VER_SUITE_ENTERPRISE As Short = &H2 'Windows Server 2008 Enterprise, Windows Server 2003, Enterprise Edition, or Windows 2000 Advanced Server
Private Const VER_SUITE_BLADE As Short = &H400 'Windows Server 2003, Web Edition
Private Const VER_SUITE_WH_SERVER As Long = &H8000 'Windows Home Server
'wProductType定義値
Private Const VER_NT_WORKSTATION As Short = &H1 'Windows 8, Windows 7, Windows Vista, Windows XP Professional, Windows XP Home Edition, or Windows 2000 Professional
Private Const VER_NT_DOMAIN_CONTROLLER As Short = &H2 'Windows Server 2012 , Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, or Windows 2000 Server
Private Const VER_NT_SERVER As Short = &H3 'Windows Server 2012, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, or Windows 2000 Server
'GetSystemInfoを使うための定義
Private Structure SYSTEM_INFO
Dim wProcessorArchitecture As Long
Dim wReserved As Long
Dim dwPageSize As Long
Dim lpMinimumApplicationAddress As Long
Dim lpMaximumApplicationAddress As Long
Dim dwActiveProcessorMask As Long
Dim dwNumberOrfProcessors As Long
Dim dwProcessorType As Long
Dim dwAllocationGranularity As Long
Dim dwReserved As Long
End Structure
Private Declare Function GetSystemInfo Lib "kernel32.dll" (ByRef o As SYSTEM_INFO) As Integer
'wProcessorArchitecture定義値
Private Const PROCESSOR_ARCHITECTURE_AMD64 As Short = 9 'x64 (AMD or Intel)
Private Const PROCESSOR_ARCHITECTURE_IA64 As Short = 6 'Intel Itanium-based
Private Const PROCESSOR_ARCHITECTURE_INTEL As Short = 0 'x86
Private Const PROCESSOR_ARCHITECTURE_UNKNOWN As Long = &HFFFF 'Unknown architecture
'dwProcessorType定義値
Private Const PROCESSOR_INTEL_386 As Short = 386
Private Const PROCESSOR_INTEL_486 As Short = 486
Private Const PROCESSOR_INTEL_PENTIUM As Short = 586
Private Const PROCESSOR_INTEL_IA64 As Short = 2200
Private Const PROCESSOR_AMD_X8664 As Short = 8664
'GetSystemMetricsを使うための定義
Private Declare Function GetSystemMetrics% Lib "user32.dll" (ByVal nIndex%)
'nIndex定義値
Private Const SM_SERVERR2 As Integer = 89 'Windows Server 2003 R2
Private Const SM_MEDIACENTER As Integer = 87 'Windows XP Media Center Edition
Private Const SM_STARTER As Integer = 88 'Windows XP Starter Edition
Private Const SM_TABLETPC As Integer = 86 'Windows XP Tablet PC Edition
'OSバージョン取得関数
Function GetOsVersion() As String
'OSバージョン返却
Return Environment.OSVersion.ToString
End Function
'OS詳細バージョン取得関数
Function GetOsDetailVersion() As String
'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
'GetSystemInfoでシステム情報取得
Dim sysinfo As SYSTEM_INFO
GetSystemInfo(sysinfo)
'OS名初期化
Dim windowsName As String = "Unknown Windows"
'OSバージョン判定
Select Case osInfo.dwPlatformId
Case VER_PLATFORM_WIN32_NT 'Windows NT系
If osInfo.dwMajorVersion = 5 Then
Select Case osInfo.dwMinorVersion
Case 0
windowsName = "Windows 2000"
Case 1
windowsName = "Windows XP"
Case 2
If osInfo.wProductType = VER_NT_WORKSTATION And
sysinfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64 Then
windowsName = "Windows XP Professional x64 Edition"
ElseIf GetSystemMetrics(SM_SERVERR2) = 0 Then
windowsName = "Windows Server 2003"
ElseIf (osInfo.wSuiteMask And VER_SUITE_WH_SERVER) = VER_SUITE_WH_SERVER Then
windowsName = "Windows Home Server"
ElseIf GetSystemMetrics(SM_SERVERR2) <> 0 Then
windowsName = "Windows Server 2003 R2"
End If
End Select
ElseIf osInfo.dwMajorVersion = 6 Then
Select Case osInfo.dwMinorVersion
Case 0
If osInfo.wProductType = VER_NT_WORKSTATION Then
windowsName = "Windows Vista"
ElseIf osInfo.wProductType <> VER_NT_WORKSTATION Then
windowsName = "Windows Server 2008"
End If
Case 1
If osInfo.wProductType <> VER_NT_WORKSTATION Then
windowsName = "Windows Server 2008 R2"
ElseIf osInfo.wProductType = VER_NT_WORKSTATION Then
windowsName = "Windows 7"
End If
Case 2
If osInfo.wProductType <> VER_NT_WORKSTATION Then
windowsName = "Windows Server 2012"
ElseIf osInfo.wProductType = VER_NT_WORKSTATION Then
windowsName = "Windows 8"
End If
Case 3
If osInfo.wProductType <> VER_NT_WORKSTATION Then
windowsName = "Windows Server 2012 R2 Preview"
ElseIf osInfo.wProductType = VER_NT_WORKSTATION Then
windowsName = "Windows 8.1 Preview"
End If
End Select
End If
End Select
'OSバージョン返却
Return windowsName
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment