gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:18:22 27 4
gpt4 key购买 nike

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

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

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

最佳答案

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.

以下代码将返回一个字符串值,指示当前的 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++ 编写了该代码,文档非常容易理解。


通过 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

关于windows - 如何从 VB 6 应用程序确定 Windows 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839210/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com