简体   繁体   English

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

[英]How can I determine the Windows version from a VB 6 app?

I want to detect any Windows versions from 95 to Win 7. 我想检测从95到Win 7的任何Windows版本。

I also would like to display if the OS is 32-bit or 64-bit. 如果操作系统是32位或64位,我也想显示。

That's it; 而已; it's that simple. 就这么简单。 :) What code could I use to do this from within a VB 6 application? :)我可以使用什么代码在VB 6应用程序中执行此操作?

Update: For code that correctly detects Windows 8.1 and Windows 10, see this answer . 更新:对于正确检测Windows 8.1和Windows 10的代码,请参阅此答案

The code below still works fine for older versions of Windows, but it will report anything newer than Windows 8 as being Windows 8. 下面的代码仍适用于旧版本的Windows,但它会报告比Windows 8更新的任何东西,因为它是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. 底部显示的“位数”测试代码(查看操作系统是32位还是64位仍然有效,即使在Windows 10上也是如此。

The following code will return a string value indicating the current version of Windows. 以下代码将返回一个字符串值,指示当前版本的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. 基本上,它所做的就是使用GetVersionEx API函数从Windows获取系统版本号,然后将这些版本与已知版本的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.) (请注意,某些内容未被完美检测到。例如,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

Additionally, if you don't need to target the earliest versions of Windows, you can get more information by passing the OSVERSIONINFOEX structure instead. 此外,如果您不需要定位最早版本的Windows,则可以通过传递OSVERSIONINFOEX结构来获取更多信息。 I just wrote that code in C++, and the documentation is surprisingly easy to follow. 我刚用C ++编写了这段代码,文档非常容易理解。


Determining if the host OS is 32-bit or 64-bit from a VB 6 executable is a little trickier. 确定主机操作系统是否是VB 6可执行文件的32位或64位有点棘手。 The reason is because VB 6 can't compile 64-bit applications. 原因是因为VB 6无法编译64位应用程序。 Everything you write in VB 6 will run as a 32-bit application. 您在VB 6中编写的所有内容都将作为32位应用程序运行。 And 32-bit applications run on 64-bit versions of Windows in the Windows-on-Windows (WOW64) subsystem. 32位应用程序在Windows-on-Windows(WOW64)子系统中的64位版本的Windows上运行。 They will always report the current version of Windows as 32-bit, because that's what they see. 他们总是会将当前版本的Windows报告为32位,因为这就是他们所看到的。

We can work around this by initially assuming that the host OS is 32-bit, and attempting to prove this wrong. 我们可以通过最初假设主机操作系统是32位来解决这个问题,并试图证明这是错误的。 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

There's also the WMI Tasks for Operating Systems . 还有操作系统WMI任务

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo objOperatingSystem.Caption & "  " & objOperatingSystem.Version
Next

You can do something similar to the case statements provided by Cody Gray above to parse the Version value, or parse the plain text Caption value, which has listings like Microsoft(R) Windows(R) Server 2003, Standard Edition and Microsoft Windows 7 Professional . 您可以执行类似于上面Cody Gray提供的case语句来解析Version值,或解析纯文本Caption值,其中包含Microsoft(R) Windows(R) Server 2003, Standard EditionMicrosoft Windows 7 Professional

您可以尝试使用VB6附带的Microsoft Sysinfo控件 ,并检查OSPlatform,OSBuild和OSVersion属性是否与正确的OS版本匹配

The accepted answer worked for my application until I tried it on Windows 10. Even after updating the code for version number details as listed here it reported the wrong Windows version. 接受的答案适用于我的应用程序,直到我在Windows 10上尝试它。即使更新了此处列出的版本号详细信息的代码,它也报告了错误的Windows版本。 It turns out this is because: 原来这是因为:

Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). 未在Windows 8.1或Windows 10中显示的应用程序将返回Windows 8 OS版本值(6.2)。 Once an application is manifested for a given operating system version, GetVersionEx will always return the version that the application is manifested for in future releases. 一旦应用程序显示给定的操作系统版本,GetVersionEx将始终返回应用程序在将来的版本中显示的版本。 To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows . 要显示Windows 8.1或Windows 10 的应用程序 ,请参阅针对Windows的应用程序

So in order to get the correct Windows version to show up, it amounts to adding a section the application manifest: 因此,为了显示正确的Windows版本,它相当于在应用程序清单中添加一个部分:

   <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> 
       <application> 
           <!-- Windows 10 --> 
           <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
           <!-- Windows 8.1 -->
           <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
           <!-- Windows Vista -->
           <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> 
           <!-- Windows 7 -->
           <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
           <!-- Windows 8 -->
           <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
       </application> 
   </compatibility>

And then the GetVersionInfo API works as expected. 然后GetVersionInfo API按预期工作。 This manifest section was new as of Windows 7, I believe. 我相信,这个清单部分是Windows 7的新版本。

However, a very important caveat is that you must actually have tested your application on each operating system version that you list it as being compatible with. 但是,一个非常重要的警告是,您必须在每个操作系统版本上测试您的应用程序,并将其列为兼容。 These settings affect certain Windows functions, not only the way Windows version information is reported. 这些设置会影响某些Windows功能,而不仅仅是报告Windows版本信息的方式。

Here is a very simple method I use to determine 32 vs. 64 bit operating system: 这是一个非常简单的方法,用于确定32位与64位操作系统:

OSBits = IIf(Len(Environ$("PROGRAMFILES(X86)")) > 0, 64, 32)

In 64-bit Windows, the OS sets the environment variable "PROGRAMFILES(X86)" but it doesn't on 32-bit systems. 在64位Windows中,操作系统设置环境变量“PROGRAMFILES(X86)”,但它不在32位系统上。 It hasn't failed me yet... 它还没有让我失望......

WORK on WINDOWS 10 VB6 - not work in debug mode - work only on runtime 在WINDOWS 10 VB6上工作 - 无法在调试模式下 工作 - 仅在运行时工作

Private Declare Function RtlGetVersion Lib "ntdll" (ByRef lpVersionInformation As RTL_OSVERSIONINFOEX) As Long

Private Type RTL_OSVERSIONINFOEX
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128
End Type

call 呼叫

Dim lpVersionInformation As RTL_OSVERSIONINFOEX
lpVersionInformation.dwOSVersionInfoSize = Len(lpVersionInformation)
RtlGetVersion(lpVersionInformation)

Ah, found it! 啊,找到了! I don't personally use this class because for my needs it's overkill, but it's definitely the most thorough OpSys version example I've come across. 我个人并不使用这个课程,因为我的需求太过分了,但这绝对是我遇到过的最全面的OpSys版本。 Credit for this one goes to Kenneth Ives. 这个奖励归功于Kenneth Ives。

*I guess StackOverflow doesn't like enormous blocks of code, so the class (clsOperSystem.cls) is located in the KiCrypt Demo , an excellent compilation of hash and encryption algorithms. *我猜StackOverflow不喜欢大量的代码块,所以类(clsOperSystem.cls)位于KiCrypt Demo中 ,这是一个很好的哈希和加密算法汇编。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VB 6 应用程序如何确定它是否在 Windows 10 上运行? - How can a VB 6 app determine if it is running on Windows 10? 如何以编程方式确定 Windows PE 版本? - How can I programmatically determine Windows PE version? 如何使用批处理来确定计算机正在运行的 Windows 版本? - How can I use batch to determine what version of windows a computer is runing? 如何确定可移动驱动器上安装的Windows版本 - How do I determine the Windows version installed on a removable drive 如何从注册表中确定Windows Server 2016版本 - How to determine Windows Server 2016 version from registry 如何从非活动驱动器的批处理脚本中确定 Windows 版本? - How to determine Windows version from a batch script for an inactive drive? 如何从Windows 8.1读取嵌入式控制器版本? - how can I read embedded controller version from Windows 8.1? 如何从Windows App Store获取Windows Phone 8 App版本 - how to get windows phone 8 app version from windows app store 从 Powershell 确定操作系统版本、Linux 和 Windows - Determine the OS version, Linux and Windows from Powershell Windows 10:如何确定是否正在从网络映射的驱动器运行批处理文件 - Windows 10: How can I determine whether a batch file is being run from network mapped drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM