• Welcome to Valhalla Legends Archive.
 

Finding OS Information

Started by hismajesty, June 13, 2004, 07:54 AM

Previous topic - Next topic

hismajesty

When I read through the one I posted in the Bot Development References forum I didn't check the validity of the code posted and such. Noodlez only hinted to the types/api calls/etc that were used so I decided to rewrite it and include them here. After I finished just getting the basic OS name I wanted more information so I consulted MSDN. Anyway, this is more extensive than Noodlez code and I figure it'll be of use to somebody. I seperated the comments/code/usage to make it cleaner since it's pretty long.

Comments:
'This is an example of how to retreive your Operating System information.
'By Trust[e1] (hismajesty) http://www.clan-e1.net
'Special thanks to Noodlez for his code example at http://forum.valhallalegends.com/phpbbs/index.php?board=17;action=display;threadid=615
'All of this information can be retreived through MSDN and by using the API Viewer
'This can be easily extended to include all suites (such as BackOffice) See: OSVERSIONEX
'To extend this even more and check if it's running on a 64-bit processor see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/iswow64process.asp


'Main MSDN Resources Used:
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getversionex.asp
'http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/osversioninfo_str.asp


'Types format supplied by MSDN

'typedef struct _OSVERSIONINFO {
'DWORD dwOSVersionInfoSize;
'DWORD dwMajorVersion;
'DWORD dwMinorVersion;
'DWORD dwBuildNumber;
'DWORD dwPlatformId;
'TCHAR szCSDVersion[128];
'} OSVERSIONINFO;
'
'typedef struct _OSVERSIONINFOEX {
'DWORD dwOSVersionInfoSize;
'DWORD dwMajorVersion;
'DWORD dwMinorVersion;
'DWORD dwBuildNumber;
'DWORD dwPlatformId;
'TCHAR szCSDVersion[128];
'WORD wServicePackMajor;
'WORD wServicePackMinor;
'WORD wSuiteMask;
'BYTE wProductType;
'BYTE wReserved;
'} OSVERSIONINFOEX,
'*POSVERSIONINFOEX,
'*LPOSVERSIONINFOEX;
 
'Supplied by MSDN (Suite Information)
'VER_SUITE_BACKOFFICE Microsoft BackOffice components are installed.
'VER_SUITE_BLADE Windows Server 2003, Web Edition is installed.
'VER_SUITE_DATACENTER Windows 2000 Datacenter Server or Windows Server 2003, Datacenter Edition is installed.
'VER_SUITE_ENTERPRISE Windows NT 4.0 Enterprise Edition, Windows 2000 Advanced Server, or Windows Server 2003, Enterprise Edition is installed. Refer to the Remarks section for more information about this bit flag.
'VER_SUITE_PERSONAL Windows XP Home Edition is installed.
'VER_SUITE_SMALLBUSINESS Microsoft Small Business Server was once installed on the system, but may have been upgraded to another version of Windows. Refer to the Remarks section for more information about this bit flag.
'VER_SUITE_SMALLBUSINESS_RESTRICTED Microsoft Small Business Server is installed with the restrictive client license in force. Refer to the Remarks section for more information about this bit flag.
'VER_SUITE_TERMINAL Terminal Services is installed.
'VER_SUITE_SINGLEUSERTS Terminal Services is installed, but only one interactive session is supported.


Declares:
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Function GetVersionAdv Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFOEX) As Long


Private Const WIN95 As String = "1.4.0"
Private Const WIN98 As String = "1.4.10"
Private Const WINME As String = "1.4.98"
Private Const WINNT As String = "2.3.51" 'Windows NT 3.51
Private Const WINNT4 As String = "2.4.0" 'Windows NT 4
Private Const WIN2K As String = "2.5.0"
Private Const WINXP As String = "2.5.1"
Private Const WINNTS As String = "2.5.2" 'Windows Server 2k3


Private Type OSVERSIONINFO
   dwOSVersionInfoSize As Long 'Size = 148
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformID As Long
   szCSDVersion As String * 128
End Type

Private Type OSVERSIONINFOEX 'Only works if OS >= NT4 SP6
   dwOSVersionInfoSize As Long 'Size = 156
   dwMajorVersion As Long
   dwMinorVersion As Long
   dwBuildNumber As Long
   dwPlatformID As Long
   szCSDVersaion As String * 128
   wServicePackMajor As Integer
   wServicePackMinor As Integer
   wSuiteMask As Integer
   wProductType As Byte
   wReserved As Byte
End Type

Private Enum WinProdType
   VER_NT_WORKSTATION = 1 'System = Win XP *, WIN NT 4, WIN 2K Pro
   VER_NT_DOMAIN_CONTROLLER = 2 'System = domain controller
   VER_NT_SERVER = 3 'System = server
End Enum

Private Enum WinSuiteMask
   VER_SUITE_SMALLBUSINESS = &H1
   VER_SUITE_ENTERPRISE = &H2
   VER_SUITE_BACKOFFICE = &H4
   VER_SUITE_COMMUNICATIONS = &H8
   VER_SUITE_TERMINAL = &H10
   VER_SUITE_SMALLBUSINESS_RESTRICTED = &H20
   VER_SUITE_EMBEDDEDNT = &H40
   VER_SUITE_DATACENTER = &H80
   VER_SUITE_SINGLEUSERTS = &H100
   VER_SUITE_PERSONAL = &H200
   VER_SUITE_BLADE = &H400
End Enum

Code:
Private Function DisplayOS() As String
   Dim osvi As OSVERSIONINFO, xosvi As OSVERSIONINFOEX
   Dim SStatus As Boolean 'boolean takes less resources than Variant
   Dim xInfo As Boolean 'check if OS has extra info (ie: Home Edition)
   
   'Variables to hold operating system information
   Dim platformID As String, BuildNumber As String, MinorVer As String, _
   MajorVer As String, WindowsVer As String, WindowsDistro As String, _
   WindowsType As String, ServicePack As String
     
   osvi.dwOSVersionInfoSize = 148 'Len(osvi)
   xInfo = False
   SStatus = GetVersionEx(osvi) 'GetVersionEx API

   If SStatus = True Then 'If GetVersionEx doesn't return false
       platformID = osvi.dwPlatformID
       majorversion = osvi.dwMajorVersion
       minorversion = osvi.dwMinorVersion
       BuildNumber = osvi.dwBuildNumber
       ServicePack = osvi.szCSDVersion
       WindowsVer = platformID & "." & majorversion & "." & minorversion
       Select Case WindowsVer
           Case Is < WIN95
               WindowsDistro = "Windows 3.x"
           Case WIN95
               If (InStr(1, UCase(osvi.szCSDVersion), "B")) Or (InStr(1, UCase(osvi.szCSDVersion), "C")) Then
                   WindowsDistro = "Windows 95 OSR2"
               Else
                   WindowsDistro = "Windows 95"
               End If
           Case WIN98
               If (InStr(1, UCase(osvi.szCSDVersion), "A")) Then
                   WindowsDistro = "Windows 98 SE"
               Else
                   WindowsDistro = "Windows 98"
               End If
           Case WINME
               WindowsDistro = "Windows ME"
           Case WINNT
               WindowsDistro = "Windows NT 3.51"
           Case WINNT4
               WindowsDistro = "Windows NT 4.00"
                   If UCase(Trim(osvi.szCSDVersion)) >= "SERVICE PACK 6" Then
                       xInfo = True 'Gather More
                       GoTo xInfo   'Information
                   End If
                   
           Case WIN2K
               WindowsDistro = "Windows 2000"
                   xInfo = True
                   GoTo xInfo
           Case WINXP
               WindowsDistro = "Windows XP"
                   xInfo = True
                   GoTo xInfo
           Case WINNTS
               WindowsDistro = "Windows Server 2003"
                   xInfo = True
                   GoTo xInfo
           Case Else
               WindowsDistro = "Unknown Windows Version"
       End Select
   End If
   
xInfo:
If xInfo = True Then
   xosvi.dwOSVersionInfoSize = 156 'Len(xosvi)
   SStatus = GetVersionAdv(xosvi)
   
   If SStatus = True Then
       Select Case xosvi.dwMajorVersion
           Case &H4 'NT 4
               Select Case xosvi.wProductType
                   Case WinProdType.VER_NT_DOMAIN_CONTROLLER
                       WindowsType = "Domain Controller"
                       
                   Case WinProdType.VER_NT_SERVER
                       If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_ENTERPRISE > 0) Then
                           WindowsType = "Advanced Server"
                       Else
                           WindowsType = "Server"
                       End If
                       
                   Case WinProdType.VER_NT_WORKSTATION
                       WindowsType = "Workstation"
               End Select
           Case &H5 'WIN2K/XP/2k3
               Select Case xosvi.dwMinorVersion
                   Case &H0 'WIN2K
                       Select Case xosvi.wProductType
                           Case WinProdType.VER_NT_DOMAIN_CONTROLLER
                               WindowsType = "Domain Controller"
                           
                           Case WinProdType.VER_NT_SERVER
                               If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_ENTERPRISE > 0) Then
                                   WindowsType = "Advanced Server"
                               Else
                                   WindowsType = "Server"
                               End If
                           
                           Case WinProdType.VER_NT_WORKSTATION
                               WindowsType = "Professional"
                       End Select
                   
                   Case &H1 'XP
                       Select Case xosvi.wProductType
                           Case WinProdType.VER_NT_DOMAIN_CONTROLLER
                               WindowsType = "Domain Controller"
                               
                           Case WinProdType.VER_NT_SERVER
                               WindowsType = "Server"
                               
                           Case WinProdType.VER_NT_WORKSTATION
                               If (xosvi.wSuiteMask And WinSuiteMask.VER_SUITE_PERSONAL > 0) Then
                                   WindowsType = "Home Edition"
                               Else
                                   WindowsType = "Professional"
                               End If
                       End Select
                                                     
                   Case &H2 '2k3
                       Select Case xosvi.wProductType
                           Case WinProdType.VER_NT_DOMAIN_CONTROLLER
                               WindowsType = "Domain Controller"
                           
                           Case WinProdType.VER_NT_SERVER
                               If xosvi.wSuiteMask Then
                                   If WinSuiteMask.VER_SUITE_BLADE Then
                                       WindowsType = "Web Server"
                                   ElseIf WinSuiteMask.VER_SUITE_DATACENTER Then
                                       WindowsType = "Datacenter Server"
                                   ElseIf WinSuiteMask.VER_SUITE_ENTERPRISE Then
                                       WindowsType = "Enterprise Server"
                                   Else
                                       WindowsType = "Server" 'Personal?
                                   End If
                               End If

                           Case WinProdType.VER_NT_WORKSTATION
                               WindowsType = vbNullString
                       End Select
                       
                   Case Else
                       WindowsType = vbNullString
               End Select
           Case Else
               WindowsType = vbNullString
       End Select
   End If
   End If
   DisplayOS = WindowsDistro & Space(1) & WindowsType & Space(1) & ServicePack
End Function


Code Example:
Private Sub Form_Load()
    MsgBox DisplayOS
End Sub


You can also return the values for these variables which is probably of more use:
Dim platformID As String, BuildNumber As String, MinorVer As String,  MajorVer As String, WindowsVer As String, WindowsDistro As
String,  WindowsType As String, ServicePack As String


The code is pretty self explainitory as long as you have a basic knowledge of VB/API calls. And it looked clean in the IDE, hopefully it will stay that way when formatted on the forum.

My wrists hurt now from all that typing. Hopefully it wasn't for no reason. :P

UserLoser.

Nice, but I've never heard of  Windows .NET Server 2003 - just Windows Server 2003.


UserLoser.

Quote from: hismajesty[yL] on June 13, 2004, 01:17 PM
Microsoft refers to it as .NET

I have Windows Server 2003 installed, and nowhere do I see Windows .NET Server 2003

hismajesty

#4
Replace(thatcode, " .NET", vbNullString) :)

Edit: Actually I don't think it matters which is used since MS.com uses both in reference to the same product.

Edit #2: Ah, it appears I'm a bit outdated. According to This Microsoft retired the .NET branding in the name.