windows 我可以从 VB6 确定我是否在 Win7 操作系统上吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5980331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:50:16  来源:igfitidea点击:

Can I determine if I am on Win7 OS from VB6?

windowswindows-7vb6operating-system

提问by DeveloperM

I have an old program written in VB6 which needs to run on 3 different platforms, including my laptop which is running Win7. I googled how to determine OS from VB6 and found some code which I slightly modified as follows:

我有一个用 VB6 编写的旧程序,它需要在 3 个不同的平台上运行,包括我运行 Win7 的笔记本电脑。我用谷歌搜索了如何从 VB6 确定操作系统,并找到了一些我稍微修改如下的代码:

Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer

Public Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId 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 As Long = 2


Private Function GetOS() As String
    Dim osinfo As OSVERSIONINFO
    Dim retvalue As Integer
    Dim sOS as String

    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)

    Select Case osinfo.dwMajorVersion
        Case 7
            sOS = "?"  'Win7?
        Case 6
            sOS = "Vista"
         Case 5
            sOS = "XP"
         Case 4
            sOS = "Win2000"
     End Select

     MsgBox (sOS)
     return sOS     

End Function

When I run this from my WIN7 laptop, osinfo.dwMajorVersion = 5, which suggests it is on an XP machine.

当我从我的 WIN7 笔记本电脑上运行它时,osinfo.dwMajorVersion = 5,这表明它在 XP 机器上。

What's ocurring here? Can I determine if I am running Win7 using this method? What's the best way of getting the info I need?

这里发生了什么?我可以使用这种方法确定我是否正在运行 Win7 吗?获取我需要的信息的最佳方式是什么?

回答by Cody Gray

Windows 7 is actually version 6.1, not version 7. You're checking for the wrong number. Otherwise, I'm not really sure why the code you've shown doesn't work. At least one problem is that there is no returnkeyword in VB 6. The last line in your GetOSfunction should be GetOS = sOS, instead. Once I fix those problems, it works just fine for me as well.

Windows 7 实际上是6.1版,而不是 7 版。您正在检查错误的数字。否则,我不确定为什么您显示的代码不起作用。至少有一个问题是returnVB 6 中没有关键字。GetOS函数中的最后一行应该是GetOS = sOS, 。一旦我解决了这些问题,它对我来说也很好用。

I have a full working solution available here. It detects all known versions of Windows quickly and accurately. I've personally tested this on at least 5 different versions of Windows with nary a hitch. All that you have to do is copy and paste the code into your project, and it just works.

在这里一个完整的工作解决方案。它可以快速准确地检测所有已知版本的 Windows。我已经在至少 5 个不同版本的 Windows 上亲自测试过,没有出现任何问题。您所要做的就是将代码复制并粘贴到您的项目中,它就可以正常工作。

Here's proof that it works correctly on my Windows 7 laptop:

这是它在我的 Windows 7 笔记本电脑上正常工作的证据:

     

     

I'm really not sure why everyone is working so hard to propose alternative, partially-working solutions. This one is guaranteedto work, or your money back. If it doesn't work for you, make sure that you are notrunning the application under "Windows XP Mode"or some other virtual machine.

我真的不知道为什么每个人都如此努力地提出替代的、部分有效的解决方案。这个保证有效,否则你的钱会回来。如果它对您不起作用,请确保您没有“Windows XP 模式”或其他虚拟机下运行该应用程序。

回答by ZippyV

Windows 7 has version 6.1.7600 that's majorversion 6, minorversion 1, build 7600 in your code. The reason why you are seeing MajorVersion 5 is probably because of a compatibility setting. Right-click your .exe, select properties and look in the "compatibility" tab.

Windows 7 的版本为 6.1.7600,即您的代码中的majorversion 6、minorversion 1、build 7600。您看到 MajorVersion 5 的原因可能是因为兼容性设置。右键单击您的 .exe,选择属性并查看“兼容性”选项卡。

回答by Ray

I tried all the API calls and code but always ended up with Windows XP for some or other reason. Used this "hack" to solve my problem and it works for me.

我尝试了所有 API 调用和代码,但由于某些或其他原因,总是以 Windows XP 结束。使用这个“hack”来解决我的问题,它对我有用。

Private Function GetMyWindowsVersion() As String    
Dim r As Long, bFile As Integer, verString As String, fResult As String, bracketStart As Integer, verInfo As String, bracketEnd As Integer, versionLength As Integer

fResult = "Windows OS"

bFile = FreeFile
Open App.Path & "\checkos.bat" For Output As #bFile    
    Print #bFile, "@echo off"    
    Print #bFile, "ver > version.txt"    
    Print #bFile, "exit"    
Close #bFile

r = Shell(App.Path & "\checkos.bat", vbMinimizedNoFocus)

bFile = FreeFile    
Open App.Path & "\version.txt" For Input As #bFile    
    Do Until EOF(bFile)    
        Line Input #bFile, verString    
        If Trim(verString) <> "" Then    
            bracketStart = InStr(verString, "[")    
            bracketEnd = InStr(verString, "]")    
            If bracketStart And bracketEnd > 0 Then    
                versionLength = bracketEnd - bracketStart    
                verInfo = Mid(verString, bracketStart + 1, versionLength - 1)    
                If InStr(verString, "6.2") Then    
                    fResult = "Windows 8 " & verInfo    
                End If    
                If InStr(verString, "6.1") Then    
                    fResult = "Windows 7 " & verInfo    
                End If    
                If InStr(verString, "5.") Then    
                    fResult = "Windows XP " & verInfo    
                End If    
                Exit Do    
                Else    
                fResult = verString    
                Exit Do    
            End If    
        End If    
    Loop    
Close #bFile    
GetMyWindowsVersion = fResult    
End Function

回答by Steve

This is what you are looking for....

这就是你要找的......

Option Explicit



Private Const VER_PLATFORM_WIN32s = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Private Const VER_NT_WORKSTATION = 1
Private Const VER_NT_DOMAIN_CONTROLLER = 2
Private Const VER_NT_SERVER = 3

Private Type OSVERSIONINFOEX
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
    wServicePackMajor As Integer
    wServicePackMinor As Integer
    wSuiteMask As Integer
    wProductType As Byte
    wReserved As Byte
End Type

Private Declare Function GetVersionExA Lib "kernel32" (ByRef lpVersionInformation As OSVERSIONINFOEX) As Long




Public Function GetWindowsVersion() As String
    Dim osinfo As OSVERSIONINFOEX
    Dim retvalue As Integer

    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)

    With osinfo
        Select Case .dwPlatformId
            Case 1
                Select Case .dwMinorVersion
                    Case 0:         GetWindowsVersion = "Windows 95"
                    Case 10:        GetWindowsVersion = "Windows 98"
                    Case 90:        GetWindowsVersion = "Windows Millenium"
                End Select

            Case 2
                Select Case .dwMajorVersion
                    Case 3:         GetWindowsVersion = "Windows NT 3.51"
                    Case 4:         GetWindowsVersion = "Windows NT 4.0"

                    Case 5
                        Select Case .dwMinorVersion
                            Case 0: GetWindowsVersion = "Windows 2000"
                            Case 1: GetWindowsVersion = "Windows XP"
                            Case 2: GetWindowsVersion = "Windows 2003"
                        End Select

                    Case 6
                        Select Case .dwMinorVersion
                            Case 0: GetWindowsVersion = "Windows Vista/2008"
                            Case 1: GetWindowsVersion = "Windows 7/2008 R2"
                            Case 2: GetWindowsVersion = "Windows 8/2012"
                            Case 3: GetWindowsVersion = "Windows 8.1/2012 R2"
                        End Select
                End Select

            Case Else
                GetWindowsVersion = "Failed"
        End Select
    End With
End Function

回答by Zamboni

Take a look at the following site. This works for detecting Vista and Windows 2008, with some minor enhancement it should work for Windows 7.

看看下面的网站。这适用于检测 Vista 和 Windows 2008,有一些小的增强它应该适用于 Windows 7。

回答by Alex K.

Your lookups are wrong; dwMajorVersion 5is win2k orXP, 6is Server 2k8 R2 orWin 7 - you need to take into account dwMinorVersionto make your detection accurate. (Table of values)

你的查找是错误的;dwMajorVersion 5是win2k还是XP,6是Server 2k8 R2还是Win 7——你需要考虑dwMinorVersion到让你的检测准确。(数值表

回答by Krammig

Thanks for the code. However I have tried this on Windows 7 Ultimate and it reports as "XP" with "version 5.1"?

感谢您的代码。但是我已经在 Windows 7 Ultimate 上尝试过这个,它报告为“XP”和“版本 5.1”?

Ok I have just tried the following and it seems to be working fine. This is using the MS SysInfo control.

好的,我刚刚尝试了以下操作,它似乎工作正常。这是使用 MS SysInfo 控件。

Private Sub Command2_Click()

    Dim MsgEnd As String
    Select Case SysDetectOS.OSPlatform
        Case 0
            MsgEnd = "Unidentified"
        Case 1
            MsgEnd = "Windows 95, ver. " & _
                     CStr(SysDetectOS.OSVersion) & "(" & _
                     CStr(SysDetectOS.OSBuild) & ")"
        Case 2
            MsgEnd = "Windows NT, ver. " & _
                     CStr(SysDetectOS.OSVersion) & "(" & _
                     CStr(SysDetectOS.OSBuild) & ")"

            If SysDetectOS.OSVersion >= 6.01 Then
                MsgEnd = MsgEnd + " Win7"
            End If        
    End Select
    MsgBox "System: " & MsgEnd    
End Sub