簡體   English   中英

如何從VB 6應用程序中確定Windows版本?

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

我想檢測從95到Win 7的任何Windows版本。

如果操作系統是32位或64位,我也想顯示。

而已; 就這么簡單。 :)我可以使用什么代碼在VB 6應用程序中執行此操作?

更新:對於正確檢測Windows 8.1和Windows 10的代碼,請參閱此答案

下面的代碼仍適用於舊版本的Windows,但它會報告比Windows 8更新的任何東西,因為它是Windows 8。

底部顯示的“位數”測試代碼(查看操作系統是32位還是64位仍然有效,即使在Windows 10上也是如此。

以下代碼將返回一個字符串值,指示當前版本的Windows。 基本上,它所做的就是使用GetVersionEx API函數從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結構來獲取更多信息。 我剛用C ++編寫了這段代碼,文檔非常容易理解。


確定主機操作系統是否是VB 6可執行文件的32位或64位有點棘手。 原因是因為VB 6無法編譯64位應用程序。 您在VB 6中編寫的所有內容都將作為32位應用程序運行。 32位應用程序在Windows-on-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

還有操作系統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

您可以執行類似於上面Cody Gray提供的case語句來解析Version值,或解析純文本Caption值,其中包含Microsoft(R) Windows(R) Server 2003, Standard EditionMicrosoft Windows 7 Professional

您可以嘗試使用VB6附帶的Microsoft Sysinfo控件 ,並檢查OSPlatform,OSBuild和OSVersion屬性是否與正確的OS版本匹配

接受的答案適用於我的應用程序,直到我在Windows 10上嘗試它。即使更新了此處列出的版本號詳細信息的代碼,它也報告了錯誤的Windows版本。 原來這是因為:

未在Windows 8.1或Windows 10中顯示的應用程序將返回Windows 8 OS版本值(6.2)。 一旦應用程序顯示給定的操作系統版本,GetVersionEx將始終返回應用程序在將來的版本中顯示的版本。 要顯示Windows 8.1或Windows 10 的應用程序 ,請參閱針對Windows的應用程序

因此,為了顯示正確的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>

然后GetVersionInfo API按預期工作。 我相信,這個清單部分是Windows 7的新版本。

但是,一個非常重要的警告是,您必須在每個操作系統版本上測試您的應用程序,並將其列為兼容。 這些設置會影響某些Windows功能,而不僅僅是報告Windows版本信息的方式。

這是一個非常簡單的方法,用於確定32位與64位操作系統:

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

在64位Windows中,操作系統設置環境變量“PROGRAMFILES(X86)”,但它不在32位系統上。 它還沒有讓我失望......

在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

呼叫

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

啊,找到了! 我個人並不使用這個課程,因為我的需求太過分了,但這絕對是我遇到過的最全面的OpSys版本。 這個獎勵歸功於Kenneth Ives。

*我猜StackOverflow不喜歡大量的代碼塊,所以類(clsOperSystem.cls)位於KiCrypt Demo中 ,這是一個很好的哈希和加密算法匯編。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM