2011-01-29 62 views

回答

27

更新:对于正确检测的Windows 8.1和Windows 10码,见this answer

下面仍然工作正常,旧版本的Windows,但它会为Windows 8的

在底部显示的“位数”的测试代码比Windows 8报告任何新的代码(查看是否操作系统即使在Windows 10上,32位或64位仍然有效。

以下代码将返回一个字符串值,指示当前版本的Windows。基本上,它所做的只是从Windows获取系统版本号使用GetVersionEx API function,然后将这些匹配到已知版本的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 
+0

osv.OSVSize = LEN(OSV)让我在VB2010Espress一个错误:它已被分配AV之前变量“OSV”用来ALUE .... – 2011-08-22 12:13:17

+0

@RemusRigo:**是的,当然**这段代码是* VB 6 *,自2002年以来(称为VB 7)更新回到1998年左右的Visual Basic的每个版本的旧版本最后有基于.NET Framework,甚至最初称为VB.NET。他们已经因为放弃了“.NET”部分,因为每个人都完全忘记了VB 6。它甚至不是由微软支持了,因为它是近15岁。 VB 6和VB.NET *完全不同,并且在一个**中工作的代码在另一个中不起作用。你需要编写.NET代码。你正在看错标签的问题。 – 2011-08-22 12:15:57

0

啊,发现它!我不亲自使用这个类,因为我的需要它是过度的,但它绝对是我遇到的最全面的OpSys版本示例。这一个信贷去Kenneth Ives。

*我猜StackOverflow不喜欢巨大的代码块,所以类(clsOperSystem.cls)位于KiCrypt Demo,这是散列和加密算法的优秀汇编。

2

你可以尝试使用自带VB6微软Sysinfo control和检查OSPlatform,OSBuild和OSVERSION propertys,以配合适当的OS Version #

4

还有WMI Tasks for Operating Systems

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, Standard EditionMicrosoft Windows 7 Professional上市的case语句的东西。

1

接受的答案工作了我的申请,直到我甚至为更新版本号的代码之后试了一下在Windows 10细节as listed here该公司报告了错误的Windows版本。事实证明,这是因为:

不适用于Windows 8.1或Windows 10的应用程序将返回Windows 8 OS版本值(6.2)。一旦应用程序表现为给定的操作系统版本,GetVersionEx将始终返回应用程序在未来版本中显示的版本。要显示Windows 8.1或Windows 10的应用程序,请参阅Targeting your application for 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版本信息的报告方式。

1

这里是一个非常简单的方法我用它来确定32与64位操作系统:

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

在64位Windows中,OS设置环境变量“PROGRAMFILES(X86)”,但它不不适用于32位系统。它没有让我失望尚未...

0

工作在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) 
相关问题