Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222057
  • 博文数量: 88
  • 博客积分: 3020
  • 博客等级: 中校
  • 技术积分: 707
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-12 16:56
文章分类
文章存档

2010年(26)

2009年(62)

我的朋友

分类: WINDOWS

2009-05-18 14:50:38

本代码能够检测各windows版本,包括95/95 osr2/98/98 se/win me/nt(workstation/server)/2000(pro/adv server/data center)/xp
Option Explicit

Public Type OSVERSIONINFOEX
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128    ' Maintenance string for PSS usage
    wServicePackMajor As Integer
    wServicePackMinor As Integer
    wSuiteMask As Integer
    wProductType As Byte
    wReserved As Byte
End Type

Public Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128      '  Maintenance string for PSS usage
End Type

Public Const VER_PLATFORM_WIN32s = 0
Public Const VER_PLATFORM_WIN32_WINDOWS = 1
Public Const VER_PLATFORM_WIN32_NT = 2

Public Const VER_NT_WORKSTATION = 1
Public Const VER_NT_DOMAIN_CONTROLLER = 2
Public Const VER_NT_SERVER = 3


Public Const VER_SERVER_NT = &H80000000
Public Const VER_WORKSTATION_NT = &H40000000
Public Const VER_SUITE_ENTERPRISE = &H2
Public Const VER_SUITE_DATACENTER = &H80
Public Const VER_SUITE_PERSONAL = &H200

Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFOEX) As Long
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long

Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public Const HKEY_LOCAL_MACHINE = &H80000002

Public Const KEY_QUERY_VALUE = &H1




Public Function GetWinVer() As String
    Dim osi As OSVERSIONINFOEX, osi1 As OSVERSIONINFO, bOsVersionInfoEx As Boolean, strVer As String
    osi.dwOSVersionInfoSize = Len(osi)
    bOsVersionInfoEx = GetVersionEx(osi)
    If (bOsVersionInfoEx = 0) Then
        osi.dwOSVersionInfoSize = Len(osi1)
        Call GetVersionEx(osi)
    End If
    Select Case osi.dwPlatformId
        Case VER_PLATFORM_WIN32_NT:
            If (osi.dwMajorVersion <= 4) Then
strVer = "Microsoft Windows NT"
ElseIf (osi.dwMajorVersion = 5 And osi.dwMinorVersion = 0) Then
strVer = "Microsoft Windows 2000"
ElseIf (osi.dwMajorVersion = 5 And osi.dwMinorVersion = 1) Then
strVer = "Microsoft Windows XP"
End If

If (bOsVersionInfoEx) Then
If (osi.wProductType = VER_NT_WORKSTATION) Then
If (osi.wSuiteMask And VER_SUITE_PERSONAL) Then
strVer = strVer & " Personal"
Else
strVer = strVer & " Professional"
End If
ElseIf (osi.wProductType = VER_NT_SERVER) Then
If (osi.wSuiteMask And VER_SUITE_DATACENTER) Then
strVer = strVer & " DataCenter Server"
ElseIf (osi.wSuiteMask And VER_SUITE_ENTERPRISE) Then
strVer = strVer & " Advanced Server"
Else
strVer = strVer & " Server"
End If
End If
Else
Dim hKey As Long
Dim szProductType As String * 80
Dim dwBufLen As Long

Call RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\ProductOptions", 0, KEY_QUERY_VALUE, hKey)
Call RegQueryValueEx(hKey, "ProductType", 0&, 0&, ByVal szProductType, dwBufLen)
Call RegCloseKey(hKey)
If (szProductType = "WINNT") Then
strVer = strVer & " Professional"
ElseIf (szProductType = "LANMANNT") Then
strVer = strVer & " Server"
ElseIf (szProductType = "SERVERNT") Then
strVer = strVer & " Advanced Server"
End If
End If


If (osi.dwMajorVersion <= 4) Then
strVer = strVer & " Version" & CStr(osi.dwMajorVersion) & "." & CStr(osi.dwMinorVersion) & " " & CStr(osi.szCSDVersion) & " (Build " & CStr((osi.dwBuildNumber And &HFFFF)) & ")"
Else
strVer = strVer & " " & CStr(Left(osi.szCSDVersion, InStr(1, osi.szCSDVersion, Chr(0)) - 1)) & "(Build " & CStr(osi.dwBuildNumber And &HFFFF) & ")"
End If

Case VER_PLATFORM_WIN32_WINDOWS:
If (osi.dwMajorVersion = 4 And osi.dwMinorVersion = 0) Then
strVer = "Microsoft Windows 95"
If (Mid(osi.szCSDVersion, 2, 1) = "C" Or Mid(osi.szCSDVersion, 2, 1) = "B") Then
strVer = strVer & " OSR2"
End If
ElseIf (osi.dwMajorVersion = 4 And osi.dwMinorVersion = 10) Then
strVer = strVer & "Microsoft Windows 98"
If (Mid(osi.szCSDVersion, 2, 1) = "A") Then
strVer = strVer & " SE"
End If
ElseIf (osi.dwMajorVersion = 4 And osi.dwMinorVersion = 90) Then
strVer = strVer & "Microsoft Windows Me"
End If
Case VER_PLATFORM_WIN32s:
strVer = "Microsoft Win32s"
Case Else:
strVer = ""
End Select
GetWinVer = strVer
End Function
阅读(957) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~