我也想显示操作系统是32位还是64位。
就是这样,很简单。 :) 我能用什么代码在VB 6应用程序内部实现这个功能?
GetVersionEx
API函数从Windows获取系统版本号,然后将其与已知的Windows版本匹配。
(请注意,有些东西无法完全检测出来。例如,Windows XP的64位版本可能会被报告为Server 2003。例如,用于确定用户是否运行Windows Vista或Server 2008的代码也没有编写。但您可以根据需要对其进行微调。)
更新: 有关正确检测Windows 8.1和Windows 10的代码,请参见此答案。
下面的代码仍然适用于旧版本的Windows,但它将把任何新于Windows 8的东西报告为Windows 8。
底部显示的“比特率”测试代码(查看操作系统是32位还是64位仍然有效,即使在Windows 10上也是如此。
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
OSVERSIONINFOEX
结构来获得更多信息。我刚刚用C ++编写了该代码,并且文档非常易于理解。
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
Version
值,或者解析纯文本Caption
值,该值具有像Microsoft(R) Windows(R) Server 2003,标准版
和Microsoft Windows 7 Professional
这样的列表。为了获得正确的Windows版本显示,需要添加一个应用程序清单部分。未针对Windows 8.1或Windows 10发表表态的应用程序将返回Windows 8操作系统版本值(6.2)。一旦针对特定操作系统版本表达应用程序的表态,GetVersionEx在未来发布中将始终返回该应用程序表达的版本。若要使您的应用程序适用于Windows 8.1或Windows 10,请参阅为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)
适用于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)
啊,找到了!我个人不使用这个类,因为对于我的需求来说它有些过度,但它绝对是我遇到的最全面的操作系统版本示例。这个功劳归功于 Kenneth Ives。
*我猜 StackOverflow 不喜欢巨大的代码块,所以这个类(clsOperSystem.cls)位于KiCrypt Demo中,这是一个出色的哈希和加密算法的编译。