如何从VB 6应用程序确定Windows版本? [英] How can I determine the Windows version from a VB 6 app?

查看:168
本文介绍了如何从VB 6应用程序确定Windows版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测从95到Win 7的所有Windows版本。



我还想显示操作系统是32位还是64位。 / p>

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

解决方案


更新:有关可正确检测Windows 8.1和Windows 10的代码,请参见此答案



下面的代码对于旧版本的Windows仍然可以正常使用,但是它将报告比Windows 8更高的任何版本,即Windows8。



底部显示的 bitness测试代码(即使在Windows 10上也可以查看操作系统是32位还是64位)。


以下代码将返回一个字符串值,指示Windows的当前版本。基本上,所有要做的就是使用 GetVersionEx API函数,然后将其匹配到



(请注意,某些东西无法完美检测。例如,可能将Windows XP的64位版本报告为Server 2003。例如,还没有编写确定用户是运行Windows Vista还是Server 2008的代码。 d根据需要进行调整。)

  Option Explicit 

私有声明函数GetVersionEx Lib kernel32别名 GetVersionExA _
(lpVersionInformation as OSVERSIONINFO)只要

私有类型OSVERSIONINFO
OSVSize只要
dwVerMajor只要
dwVerMinor只要
dwBuildNumber只要
平台ID只要
szCSDVersion只要字符串* 128
终端类型

私有常量VER_PLATFORM_WIN32s = 0
私有常量VER_PLATFORM_WIN32_WINDOWS = 1
私有常量VER_PLATFORM_WIN32_NT = 2

'返回用户正在运行的Windows版本
公共函数GetWindowsVersion()作为字符串
Dim osv作为OSVERSIONINFO
osv .OSVSize = Len(osv)

如果GetVersionEx(osv)= 1然后
选择Case osv.PlatformID
Case VER_PLATFORM_WIN32s
GetWindowsVersion = Windows 3.1上为Win32s
Cas e VER_PLATFORM_WIN32_NT
GetWindowsVersion = Windows NT

选择案例osv.dwVerMajor
案例3
GetWindowsVersion = Windows NT 3.5
案例4
GetWindowsVersion = Windows NT 4.0
案例5
选择案例osv.dwVerMinor
案例0
GetWindowsVersion = Windows 2000
案例1
GetWindowsVersion = Windows XP
Case 2
GetWindowsVersion = Windows Server 2003
End Select
Case 6
Select Case osv.dwVerMinor
Case 0
GetWindowsVersion = Windows Vista / Ser ver 2008
案例1
GetWindowsVersion = Windows 7 / Server 2008 R2
案例2
GetWindowsVersion = Windows 8 / Server 2012
案例3
GetWindowsVersion = Windows 8.1 / Server 2012 R2
结束选择
结束选择

案例VER_PLATFORM_WIN32_WINDOWS:
选择案例osv.dwVerMinor
案例0
GetWindowsVersion = Windows 95
Case 90
GetWindowsVersion = Windows Me
Case其他
GetWindowsVersion = Windows 98
结束选择
结束选择
其他
GetWindowsVersion =无法识别您的Windows版本。
如果
结束函数,则结束

此外,如果您不需要定位最早的版本Windows,您可以通过传递 OSVERSIONINFOEX来获取更多信息结构。我只是用C ++编写了该代码,文档非常容易遵循。






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



我们可以通过以下方法解决此问题:首先假设主机操作系统是32位,然后尝试证明这是错误的。下面是一些示例代码:

 私有声明函数GetProcAddress Lib kernel32 _ 
(ByVal hModule As Long,ByVal lpProcName作为字符串)只要

私有声明函数GetModuleHandle Lib kernel32 _
别名 GetModuleHandleA(ByVal lpModuleName作为字符串)只要

私有声明函数GetCurrentProcess Lib kernel32()只要

私有声明函数IsWow64Process Lib kernel32 _
(ByVal hProc为Long,ByRef bWow64Process为布尔值)

公共函数IsHost64Bit()作为布尔
昏暗句柄只要
昏暗is64Bit作为布尔

'最初假定这不是WOW64进程
is64Bit = False

'然后尝试通过尝试加载
'来证明错误,IsWow64Process函数动态
handle = GetProcAddress(GetModuleHandle( kernel32), IsWow64Process)

'该函数存在,因此称它为
如果句柄<> 0然后
IsWow64Process GetCurrentProcess(),is64Bit
End如果

'返回值
IsHost64Bit = is64Bit
结束函数


I want to detect any Windows versions from 95 to Win 7.

I also would like to display if the OS is 32-bit or 64-bit.

That's it; it's that simple. :) What code could I use to do this from within a VB 6 application?

解决方案

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

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.

The following code will return a string value indicating the current version of Windows. Basically, all it's doing is getting the system version numbers from Windows using the GetVersionEx API function, and then matching those up to the known versions of Windows.

(Note that some things are not detected perfectly. For example, a 64-bit version of Windows XP would likely be reported as Server 2003. Code to determine whether the user is running Windows Vista or Server 2008, for example, has also not been written. But you can take this and tweak it as desired.)

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

Additionally, if you don't need to target the earliest versions of Windows, you can get more information by passing the OSVERSIONINFOEX structure instead. I just wrote that code in C++, and the documentation is surprisingly easy to follow.


Determining if the host OS is 32-bit or 64-bit from a VB 6 executable is a little trickier. The reason is because VB 6 can't compile 64-bit applications. Everything you write in VB 6 will run as a 32-bit application. And 32-bit applications run on 64-bit versions of Windows in the Windows-on-Windows (WOW64) subsystem. They will always report the current version of Windows as 32-bit, because that's what they see.

We can work around this by initially assuming that the host OS is 32-bit, and attempting to prove this wrong. Here's some sample code:

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

这篇关于如何从VB 6应用程序确定Windows版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆