微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从VB 6应用程序确定Windows版本?

我想检测到从95到Win 7的任何Windows版本。

我也想显示操作系统是32位还是64位。

而已;这很简单:)我可以使用什么代码在VB 6应用程序中执行此操作?

Update: For code that correctly detects Windows 8.1 and Windows 10,see 07000.

The code below still works fine for older versions of Windows,but it will report anything newer than Windows 8 as being Windows 8.

The “bitness” testing code shown at the bottom (to see if the OS is 32-bit or 64-bit still works,even on Windows 10.

以下代码将返回一个指示当前版本的Windows的字符串值。基本上,它所做的只是使用GetVersionEx API function从Windows获取系统版本号,然后将其与已知版本的Windows相匹配。

(请注意,有些事情没有完全检测到,例如,64位版本的Windows XP可能会报告为Server 2003.例如,确定用户是否运行Windows Vista或Server 2008的代码也没有被写的,但你可以采取这一点,根据需要进行调整。)

Option Explicit

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
    (lpVersioninformation As OsveRSIONINFO) As Long

Private Type OsveRSIONINFO
  OSVSize         As Long
  dwVerMajor      As Long
  dwVerMinor      As Long
  dwBuildNumber   As Long
  PlatformID      As Long
  szCSDVersion    As String * 128
End Type

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

' Returns the version of Windows that the user is running
Public Function Getwindowsversion() As String
    Dim osv As OsveRSIONINFO
    osv.OSVSize = Len(osv)

    If GetVersionEx(osv) = 1 Then
        Select Case osv.PlatformID
            Case VER_PLATFORM_WIN32s
                Getwindowsversion = "Win32s on Windows 3.1"
            Case VER_PLATFORM_WIN32_NT
                Getwindowsversion = "Windows NT"

                Select Case osv.dwVerMajor
                    Case 3
                        Getwindowsversion = "Windows NT 3.5"
                    Case 4
                        Getwindowsversion = "Windows NT 4.0"
                    Case 5
                        Select Case osv.dwVerMinor
                            Case 0
                                Getwindowsversion = "Windows 2000"
                            Case 1
                                Getwindowsversion = "Windows XP"
                            Case 2
                                Getwindowsversion = "Windows Server 2003"
                        End Select
                    Case 6
                        Select Case osv.dwVerMinor
                            Case 0
                                Getwindowsversion = "Windows Vista/Server 2008"
                            Case 1
                                Getwindowsversion = "Windows 7/Server 2008 R2"
                            Case 2
                                Getwindowsversion = "Windows 8/Server 2012"
                            Case 3
                                Getwindowsversion = "Windows 8.1/Server 2012 R2"
                        End Select
                End Select

            Case VER_PLATFORM_WIN32_WINDOWS:
                Select Case osv.dwVerMinor
                    Case 0
                        Getwindowsversion = "Windows 95"
                    Case 90
                        Getwindowsversion = "Windows Me"
                    Case Else
                        Getwindowsversion = "Windows 98"
                End Select
        End Select
    Else
        Getwindowsversion = "Unable to identify your version of Windows."
    End If
End Function

另外,如果您不需要定位Windows的最早版本,则可以通过传递OSVERSIONINFOEX structure获取更多信息。我只是在C中写了这个代码,文档很容易理解。

确定主机OS是否是VB 6可执行文件的32位或64位有点棘手。原因是因为VB 6无法编译64位应用程序。您在VB 6中编写的所有内容将作为32位应用程序运行。而32位应用程序则在Windows Windows(WOW64)子系统中的64位版本的Windows上运行。他们将始终将当前版本的Windows报告为32位,因为这是他们看到的。

我们可以解决这个问题,最初假设主机操作系统是32位,并试图证明这一点。以下是一些示例代码

Private Declare Function GetProcAddress Lib "kernel32" _
    (ByVal hModule As Long,ByVal lpProcName As String) As Long

Private Declare Function GetModuleHandle Lib "kernel32" _
    Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function IsWow64Process Lib "kernel32" _
    (ByVal hProc As Long,ByRef bWow64Process As Boolean) As Long

Public Function IsHost64Bit() As Boolean
    Dim handle As Long
    Dim is64Bit As Boolean

    ' Assume initially that this is not a WOW64 process
    is64Bit = False

    ' Then try to prove that wrong by attempting to load the
    ' IsWow64Process function dynamically
    handle = GetProcAddress(GetModuleHandle("kernel32"),"IsWow64Process")

    ' The function exists,so call it
    If handle <> 0 Then
        IsWow64Process GetCurrentProcess(),is64Bit
    End If

    ' Return the value
    IsHost64Bit = is64Bit
End Function

原文地址:https://www.jb51.cc/vb/256027.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐