All of us must have come across the fact that some set of codes are specific to a particular OS only and incompatible with others. In some instances they may also lead to system crashes.
In such cases we need to track and identify the OS first and then proceed accordingly. So, here is how you can write a program in VB6.0 that can run in different Windows operating systems.
The first step is to make some simple declarations. Add the following code to your projects declarations.
Private Const VER_PLATFORM_WIN32s = 0 Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT As Long = 2
Private Declare Function GetVersionEx Lib “kernel32” Alias “GetVersionExA” (lpVersionInformation As Any) 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
After we are done with the above declaration we can have some methods/functions to determine current OS as shown below.
# 1.
Public Function IdentifyOperatingSystem() As String Dim rOsVersionInfo As OSVERSIONINFO Dim sOperatingSystem As String sOperatingSystem = "NONE" rOsVersionInfo.OSVSize = Len(rOsVersionInfo) If GetVersionEx(rOsVersionInfo) Then
Select Case rOsVersionInfo.PlatformID
Case VER_PLATFORM_WIN32_NT
If rOsVersionInfo.dwVerMajor >= 5 Then If rOsVersionInfo.dwVerMinor = 0 Then sOperatingSystem = “Windows 2000” Else sOperatingSystem = “Windows XP” End If Else sOperatingSystem = “Windows NT”
End If
Case VER_PLATFORM_WIN32_WINDOWS
If rOsVersionInfo.dwVerMajor >= 5 Then sOperatingSystem = “Windows ME” ElseIf rOsVersionInfo.dwVerMajor = 4 And rOsVersionInfo.dwVerMinor > 0 Then sOperatingSystem = “Windows 98” Else sOperatingSystem = “Windows 95”
End If
Case VER_PLATFORM_WIN32s
sOperatingSystem = “Win32s”
End Select End If
End Function
# 2.
Public Function IsWinVista() As Boolean Dim osv As OSVERSIONINFO osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then IsWinVista = (osv.PlatformID = VER_PLATFORM_WIN32_NT) And (osv.dwVerMajor = 6) End If
End Function