我有一个用VB6编写的旧程序,需要在3个不同的平台上运行,包括运行Win7的笔记本电脑。我用Google搜索了如何从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
当我从WIN7笔记本电脑运行时,osinfo.dwMajorVersion = 5,这表明它在XP机器上。
这里有什么事?我可以使用此方法确定是否运行Win7吗?获取我需要的信息的最佳方式是什么?
答案 0 :(得分:8)
Windows 7实际上是版本 6.1 ,而不是版本7.您正在检查错误的号码。否则,我不确定为什么你显示的代码不起作用。至少有一个问题是VB 6中没有return
个关键字。GetOS
函数的最后一行应该是GetOS = sOS
。一旦我解决了这些问题,它对我来说也很合适。
我有a full working solution available here。它可以快速准确地检测所有已知版本的Windows。我亲自在至少5个不同版本的Windows上对此进行了测试,但没有任何障碍。 您所要做的就是将代码复制并粘贴到项目中,它就可以了。
以下是我在Windows 7笔记本电脑上正常运行的证明:
我真的不确定为什么每个人都在努力提出替代的,部分工作的解决方案。这个保证可以工作,或者你的退款。如果它不适合您,请确保您不在"Windows XP Mode"或其他虚拟机下运行该应用程序。
答案 1 :(得分:4)
Windows 7的版本6.1.7600是majorversion 6,minorversion 1,在您的代码中构建7600。您看到MajorVersion 5的原因可能是因为兼容性设置。右键单击.exe,选择属性并查看“兼容性”选项卡。
答案 2 :(得分:3)
我尝试了所有的API调用和代码,但由于某种原因或其他原因,我总是使用Windows XP。使用这个“黑客”解决我的问题,它对我有用。
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
答案 3 :(得分:0)
查看以下site。这适用于检测Vista和Windows 2008,它有一些小的增强功能,适用于Windows 7.
答案 4 :(得分:0)
你的查找错了; dwMajorVersion 5
是win2k 或 XP,6
是服务器2k8 R2 或 Win 7 - 您需要考虑dwMinorVersion
来制作你的检测准确。 (Table of values)
答案 5 :(得分:0)
这就是你要找的......
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
答案 6 :(得分:-1)
感谢您的代码。不过我在Windows 7旗舰版上尝试了这个,它报告为“XP”和“5.1版”?
好的,我刚试过以下内容,似乎工作正常。这是使用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