I want to detect any Windows versions from 95 to Win 7.
I also would like to display if the OS is 32-bit or 64-bit.
That's it; it's that simple. :) What code could I use to do this from within a VB 6 application?
I want to detect any Windows versions from 95 to Win 7.
I also would like to display if the OS is 32-bit or 64-bit.
That's it; it's that simple. :) What code could I use to do this from within a VB 6 application?
You could try using the Microsoft Sysinfo control that comes with VB6 and check for OSPlatform, OSBuild and OSVersion propertys to match with the proper OS Version #
There's also the 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
You can do something similar to the case statements provided by Cody Gray above to parse the Version
value, or parse the plain text Caption
value, which has listings like Microsoft(R) Windows(R) Server 2003, Standard Edition
and Microsoft Windows 7 Professional
.
Ah, found it! I don't personally use this class because for my needs it's overkill, but it's definitely the most thorough OpSys version example I've come across. Credit for this one goes to Kenneth Ives.
*I guess StackOverflow doesn't like enormous blocks of code, so the class (clsOperSystem.cls) is located in the KiCrypt Demo, an excellent compilation of hash and encryption algorithms.
WORK on WINDOWS 10 VB6 - not work in debug mode - work only on runtime
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
call
Dim lpVersionInformation As RTL_OSVERSIONINFOEX
lpVersionInformation.dwOSVersionInfoSize = Len(lpVersionInformation)
RtlGetVersion(lpVersionInformation)
The accepted answer worked for my application until I tried it on Windows 10. Even after updating the code for version number details as listed here it reported the wrong Windows version. It turns out this is because:
Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). Once an application is manifested for a given operating system version, GetVersionEx will always return the version that the application is manifested for in future releases. To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows.
So in order to get the correct Windows version to show up, it amounts to adding a section the application manifest:
<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>
And then the GetVersionInfo API works as expected. This manifest section was new as of Windows 7, I believe.
However, a very important caveat is that you must actually have tested your application on each operating system version that you list it as being compatible with. These settings affect certain Windows functions, not only the way Windows version information is reported.
The following code will return a string value indicating the current version of Windows. Basically, all it's doing is getting the system version numbers from Windows using the
GetVersionEx
API function, and then matching those up to the known versions of Windows.(Note that some things are not detected perfectly. For example, a 64-bit version of Windows XP would likely be reported as Server 2003. Code to determine whether the user is running Windows Vista or Server 2008, for example, has also not been written. But you can take this and tweak it as desired.)
Additionally, if you don't need to target the earliest versions of Windows, you can get more information by passing the
OSVERSIONINFOEX
structure instead. I just wrote that code in C++, and the documentation is surprisingly easy to follow.Determining if the host OS is 32-bit or 64-bit from a VB 6 executable is a little trickier. The reason is because VB 6 can't compile 64-bit applications. Everything you write in VB 6 will run as a 32-bit application. And 32-bit applications run on 64-bit versions of Windows in the Windows-on-Windows (WOW64) subsystem. They will always report the current version of Windows as 32-bit, because that's what they see.
We can work around this by initially assuming that the host OS is 32-bit, and attempting to prove this wrong. Here's some sample code: