office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

VB及VBA获取操作系统版本的通用模块

2017-09-19 08:00:00
zstmtony
原创
1224

VB及VBA获取操作系统版本的通用模块

Option Explicit

Public OSNameAs String'操作系统名称(简称),方便程序控制时根据操作系统取值

Public Type OSVERSIONINFO
    dwOSVersionInfoSize AsLong
    dwMajorVersion AsLong
    dwMinorVersion AsLong
    dwBuildNumber AsLong
    dwPlatformId AsLong
    szCSDVersion AsString * 128
    wServicePackMajor AsInteger
    wServicePackMinor AsInteger
    wSuiteMask AsInteger
    wProductType AsByte
    wReserved AsByte
End Type

Public Declare Function GetVersionExLib "kernel32"Alias "GetVersionExA" (lpVersionInformationAs OSVERSIONINFO)As Long

' 获得 Windows 操作系统的名称,returnType决定返回的方式,值为0时,返回简称,否则返回全称
Public Function GetWindowsVersion(ByVal returnTypeAs Integer)As String
    Dim ver As OSVERSIONINFO, retLngAs Long, osAs String
    ver.dwOSVersionInfoSize =Len(ver)
    GetVersionEx ver
    
'    retLng = GetVersionEx(ver)
'    If retLng = 0 Then
'        os = "Unknown System Version!"
'        Exit Function
'    End If
    
    With ver
        SelectCase .dwPlatformId
            Case1
                SelectCase .dwMinorVersion
                    Case0
                        If returnType= 0Then
                            os ="Windows 95"
                        Else
                            SelectCase .szCSDVersion
                                Case"C"
                                    os ="Windows 95 OSR2"
                                Case"B"
                                    os ="Windows 9