Attribute VB_Name = "modOS_Info" Option Explicit '============================================================================================================= ' ' modOS_Info Module ' ----------------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .net ) ' ' Created On : April 01, 2000 ' Last Update : December 16, 2003 ' ' VB Versions : 5.0 / 6.0 ' ' Requires : NOTHING ' ' Description : This module was created to easily find out what operating system the user is running the ' program on, as well as information about the OS's description and build information. ' ' See Also : http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp ' http://msdn.microsoft.com/library/en-us/sysinfo/base/getting_the_system_version.asp ' ' Example Use : ' ' Call GetOS ' If Win_OS = OS_WinNT_351 Then ' MsgBox "It's time to upgrade your operating system!" ' End If ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= ' Public Type Declarations Public Type OSVERSIONINFO dwOSVersionInfoSize As Long '// DWORD Size of this data structure, in bytes. Set this member to sizeof(OSVERSIONINFO) before calling the GetVersionEx function. dwMajorVersion As Long '// DWORD Major version number of the operating system. This member can be one of the following values: Win95 = 4, Win98 = 4, WinMe = 4, WinNT 3.51 = 3, WinNT 4.0 = 4, Win2000 = 5, WinXP = 5, Win2003 Server Family = 5 dwMinorVersion As Long '// DWORD Minor version number of the operating system. This member can be one of the following values: Win95 = 0, Win98 = 10, WinMe = 90, WinNT 3.51 = 51, WinNT 4.0 = 0, Win2000 = 0, WinXP = 1, Win2003 Server Family = 2 dwBuildNumber As Long '// DWORD Build number of the operating system. Windows Me/98/95: The low-order word contains the build number of the operating. The high-order word contains the major and minor version numbers. dwPlatformId As Long '// DWORD Operating system platform. This member can be one of the following values: VER_PLATFORM_WIN32s = Win32s on Windows 3.1, VER_PLATFORM_WIN32_WINDOWS = Windows 95/Windows 98/Windows Me, VER_PLATFORM_WIN32_NT = Windows NT/Windows 2000/Windows XP/Windows 2003 Server Family szCSDVersion As String * 128 '// TCHAR[128] Pointer to a null-terminated string, such as "Service Pack 3", that indicates the latest Service Pack installed on the system. If no Service Pack has been installed, the string is empty. Windows Me/98/95: Pointer to a null-terminated string that indicates additional version information. For example, " C" indicates Windows 95 OSR2 and " A" indicates Windows 98 Second Edition. End Type ' Public Enumeration Declarations Public Enum OSTypes OS_Unknown = 0 ' "Unknown" OS_Win32 = 32 ' "Win 32" OS_Win95 = 95 ' "Windows 95" OS_Win98 = 98 ' "Windows 98" OS_WinME = 99 ' "Windows ME" OS_WinNT_351 = 351 ' "Windows NT 3.51" OS_WinNT_40 = 40 ' "Windows NT 4.0" OS_Win2000 = 2000 ' "Windows 2000" OS_WinXP = 50 ' "Windows XP" OS_Win2003 = 2003 ' "Windows Server 2003 family" End Enum ' Public Constant Declarations Private Const VER_PLATFORM_WIN32s = 0 Private Const VER_PLATFORM_WIN32_WINDOWS = 1 Private Const VER_PLATFORM_WIN32_NT = 2 ' Public Variables Public Win_OS As OSTypes Public Win_Description As String Public Win_Version As String Public Win_SP As String Public Win_Build As String ' Win32 API Function Declarations Public Declare Function GetVersionEx Lib "KERNEL32.DLL" Alias "GetVersionExA" (ByRef lpVersionInfo As OSVERSIONINFO) As Long 'BOOL ' Function to set the windows information variables Public Function GetOS(Optional ByRef Return_ErrNum As Long, Optional ByRef Return_ErrSrc As String, Optional ByRef Return_ErrDesc As String) As Boolean On Error GoTo ErrorTrap Dim OSinfo As OSVERSIONINFO Dim strDesc As String ' Set return values to default Win_OS = OS_Unknown Win_Description = "" Win_Version = "" Win_Build = "" Return_ErrNum = 0 Return_ErrSrc = "" Return_ErrDesc = "" ' Call the API that returns the Windows version OSinfo.dwOSVersionInfoSize = Len(OSinfo) OSinfo.szCSDVersion = String(128, Chr(0)) If GetVersionEx(OSinfo) = 0 Then Return_ErrNum = Err.LastDllError Return_ErrSrc = "GetVersionEx(..)" Return_ErrDesc = "Failed to successfully get the OS version information" Err.Clear GetOS = False Exit Function End If ' Check the results With OSinfo Select Case .dwPlatformId ' Win32s Case VER_PLATFORM_WIN32s strDesc = "Win 32" Win_OS = OS_Win32 ' Windows 9x Case VER_PLATFORM_WIN32_WINDOWS If .dwMinorVersion = 0 Then strDesc = "Windows 95" Win_OS = OS_Win95 ElseIf .dwMinorVersion = 10 Then strDesc = "Windows 98" Win_OS = OS_Win98 ElseIf .dwMinorVersion = 90 Then strDesc = "Windows ME" Win_OS = OS_WinME Else strDesc = "Unknown" Win_OS = OS_Unknown End If ' Windows NT Family Case VER_PLATFORM_WIN32_NT If .dwMajorVersion = 3 Then strDesc = "Windows NT 3.51" Win_OS = OS_WinNT_351 ElseIf .dwMajorVersion = 4 Then strDesc = "Windows NT 4.0" Win_OS = OS_WinNT_40 ElseIf .dwMajorVersion = 5 Then If .dwMinorVersion = 0 Then strDesc = "Windows 2000" Win_OS = OS_Win2000 ElseIf .dwMinorVersion = 1 Then strDesc = "Windows XP" Win_OS = OS_WinXP ElseIf .dwMinorVersion = 2 Then strDesc = "Windows Server 2003 Family" Win_OS = OS_Win2003 Else strDesc = "Unknown" Win_OS = OS_Unknown End If End If ' Unknown Case Else strDesc = "Unknown" Win_OS = OS_Unknown End Select End With ' Return the information Win_Description = strDesc Win_SP = Trim(Left(OSinfo.szCSDVersion, InStr(OSinfo.szCSDVersion, Chr(0)) - 1)) Win_Version = CStr(OSinfo.dwMajorVersion) & "." & CStr(OSinfo.dwMinorVersion) Select Case Win_OS Case OS_Win95, OS_Win98, OS_WinME Win_Build = CStr(Val("&H" & Right("0000" & Hex(OSinfo.dwBuildNumber), 4))) Case Else Win_Build = CStr(OSinfo.dwBuildNumber) End Select GetOS = True Exit Function ErrorTrap: Return_ErrNum = Err.Number Return_ErrSrc = Err.Source Return_ErrDesc = Err.Description Err.Clear GetOS = False End Function '------------------------------------------------------------------------------------------------------------- ' SAMPLE CODE: '------------------------------------------------------------------------------------------------------------- ' ' OSVERSIONINFOEX osvi; ' BOOL bOsVersionInfoEx; ' ' // Try calling GetVersionEx using the OSVERSIONINFOEX structure. ' // If that fails, try using the OSVERSIONINFO structure. ' ' ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); ' osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); ' ' if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ) ' { ' osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); ' if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) ' return FALSE; ' } ' ' switch (osvi.dwPlatformId) ' { ' // Test for the Windows NT product family. ' case VER_PLATFORM_WIN32_NT: ' ' // Test for the specific product family. ' if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) ' printf ("Microsoft Windows Server 2003 family, "); ' ' if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) ' printf ("Microsoft Windows XP "); ' ' if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) ' printf ("Microsoft Windows 2000 "); ' ' if ( osvi.dwMajorVersion <= 4 ) ' printf("Microsoft Windows NT "); ' ' // Test for specific product on Windows NT 4.0 SP6 and later. ' if( bOsVersionInfoEx ) ' { ' // Test for the workstation type. ' if ( osvi.wProductType == VER_NT_WORKSTATION ) ' { ' if( osvi.dwMajorVersion == 4 ) ' printf ( "Workstation 4.0 " ); ' else if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) ' printf ( "Home Edition " ); ' else ' printf ( "Professional " ); ' } ' ' // Test for the server type. ' else if ( osvi.wProductType == VER_NT_SERVER ) ' { ' if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) ' { ' if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) ' printf ( "Datacenter Edition " ); ' else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) ' printf ( "Enterprise Edition " ); ' else if ( osvi.wSuiteMask == VER_SUITE_BLADE ) ' printf ( "Web Edition " ); ' else ' printf ( "Standard Edition " ); ' } ' ' else if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) ' { ' if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) ' printf ( "Datacenter Server " ); ' else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) ' printf ( "Advanced Server " ); ' else ' printf ( "Server " ); ' } ' ' else // Windows NT 4.0 ' { ' if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) ' printf ("Server 4.0, Enterprise Edition " ); ' else ' printf ( "Server 4.0 " ); ' } ' } ' } ' else // Test for specific product on Windows NT 4.0 SP5 and earlier ' { ' HKEY hKey; ' char szProductType[BUFSIZE]; ' DWORD dwBufLen=BUFSIZE; ' LONG lRet; ' ' lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, ' "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", ' 0, KEY_QUERY_VALUE, &hKey ); ' if( lRet != ERROR_SUCCESS ) ' return FALSE; ' ' lRet = RegQueryValueEx( hKey, "ProductType", NULL, NULL, ' (LPBYTE) szProductType, &dwBufLen); ' if( (lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE) ) ' return FALSE; ' ' RegCloseKey( hKey ); ' ' if ( lstrcmpi( "WINNT", szProductType) == 0 ) ' printf( "Workstation " ); ' if ( lstrcmpi( "LANMANNT", szProductType) == 0 ) ' printf( "Server " ); ' if ( lstrcmpi( "SERVERNT", szProductType) == 0 ) ' printf( "Advanced Server " ); ' ' printf( "%d.%d ", osvi.dwMajorVersion, osvi.dwMinorVersion ); ' } ' ' // Display service pack (if any) and build number. ' ' if( osvi.dwMajorVersion == 4 && ' lstrcmpi( osvi.szCSDVersion, "Service Pack 6" ) == 0 ) ' { ' HKEY hKey; ' LONG lRet; ' ' // Test for SP6 versus SP6a. ' lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, ' "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009", ' 0, KEY_QUERY_VALUE, &hKey ); ' if( lRet == ERROR_SUCCESS ) ' printf( "Service Pack 6a (Build %d)\n", osvi.dwBuildNumber & 0xFFFF ); ' else // Windows NT 4.0 prior to SP6a ' { ' printf( "%s (Build %d)\n", ' osvi.szCSDVersion, ' osvi.dwBuildNumber & 0xFFFF); ' } ' ' RegCloseKey( hKey ); ' } ' else // Windows NT 3.51 and earlier or Windows 2000 and later ' { ' printf( "%s (Build %d)\n", ' osvi.szCSDVersion, ' osvi.dwBuildNumber & 0xFFFF); ' } ' ' break; ' ' // Test for the Windows 95 product family. ' case VER_PLATFORM_WIN32_WINDOWS: ' ' if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) ' { ' printf ("Microsoft Windows 95 "); ' if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' ) ' printf("OSR2 " ); ' } ' ' if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) ' { ' printf ("Microsoft Windows 98 "); ' if ( osvi.szCSDVersion[1] == 'A' ) ' printf("SE " ); ' } ' ' if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) ' { ' printf ("Microsoft Windows Millennium Edition\n"); ' } ' break; ' ' case VER_PLATFORM_WIN32s: ' ' printf ("Microsoft Win32s\n"); ' break; ' } ' return TRUE; ' '-------------------------------------------------------------------------------------------------------------